系统级编程:Solution_week07.doc_第1页
系统级编程:Solution_week07.doc_第2页
系统级编程:Solution_week07.doc_第3页
系统级编程:Solution_week07.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1. Are there any memory errors in the following programs? If so, list all of them. Assume that the user enters in correct input, and that the sizes entered are at least one. Write your solution in a text or Word file and submit it below. void main() char *str, *input;int *ilist;int i, size1, size2;printf(Number of letters in word: );scanf(%d, &size1); /* user inputs an integer */printf(Number of integers: );scanf(%d, &size2); /* user inputs an integer */str = (char *) malloc(size1);ilist = (int *) malloc(size2);改为:str = (char *) malloc(size1*sizeof(char)+1);ilist = (int *) malloc(size2*sizeof(int);printf(Word: );scanf(%s, str); /* user inputs a string */for(i = 0; i size2; i+) printf(Number %d of %d: , i + 1, size2);scanf(%d, ilist + i); /* user inputs an integer */free(str);free(ilist);str = nullilist = null;2. Are there any memory errors in the following program? If so, list all of them. Write your solution in a text or Word file and submit it below. /* return 1 if str is 1, 0 otherwise */int checkIf1(char *str) char *newstr = (char *)malloc(strlen(str) + 1);strcpy(newstr, str); /* set newstr to str */if (strcmp(newstr, 1) = 0) /* newstr is 1 */free(newstr);newstr = null;return 1;free(newstr);newstr = null;return 0;void main() char *strArr4 = 1, 2, 3, 4;int i;for(i = 0; i 4; i+) printf(%dn, checkIf1(strArri);3. Are there any memory errors in the following program? If so, list all of them. Write your solution in a text or Word file and submit it below. struct data char *str1, *str2; /* returns two strings concatenated if they are not the same, NULL otherwise */char *mergeSingleIfDifferent(char *s1, char *s2) char *str = (char *) malloc(strlen(s1) + strlen(s2) + 1);if (strcmp(s1, s2) = 0) /* strings are equal */str = NULL;else strcpy(str, s1);strcat(str, s2);return str;/* copies merged strings (or NULL) into array of strings passed in (results) */void mergeArrayIfDifferent(char *results, char *strA1, char *strA2, int size) int i;for(i = 0; i size; i+) resultsi = mergeSingleIfDifferent(strA1i, strA2i); void printAndFree(int c, char *str) if (str != NULL) printf(%d: %sn, c, str);free(str);elsefree(str);void main() char *strArr18 = 1, 2, 3, 4, 5, 6, 7, 8;char *strArr28 = a, 2, c, 4, e, 6, g, 8;char *results8;int i;mergeArrayIfDifferent(results, strArr1, strArr2, 8);for(i = 0; i 8; i+) printAndFree(i, resultsi);void GetMemory(char *p)p = (char *)malloc(100);void Test(void) char *str = NULL;GetMemory(str);strcpy(str, hello world);printf(str);请问运行Test函数会有什么样的结果?答:程序崩溃。因为GetMemory并不能传递动态内存,Test函数中的 str一直都是 NULL。strcpy(str, hello world);将使程序崩溃。char *GetMemory(void)char p = hello world;return p;void Test(void)char *str = NULL;str = GetMemory();printf(str);请问运行Test函数会有什么样的结果?答:可能是乱码。因为GetMemory返回的是指向“栈内存”的指针,该指针的地址不是 NULL,但其原现的内容已经被清除,新内容不可知。void GetMemory2(char *p, int num)*p = (char *)malloc(num);void Test(void)char *str = NULL;GetMemory(&str, 100);strcpy(str, hello);printf(str);请问运行Test函数会有什么样的结果?答:(1)能够输出hello(2)内存泄漏void Test(void)char *str = (char *) malloc(100);strcpy(str,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论