《2022年系统级编程:Solution_week .pdf》由会员分享,可在线阅读,更多相关《2022年系统级编程:Solution_week .pdf(4页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、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
2、; 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(siz
3、e2*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 = null ilist = null; 2. Are there any memory errors in the following program? If
4、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 */ 名师资料总结 - - -
5、精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 4 页 - - - - - - - - - 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 erro
6、rs 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 *)
7、 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
8、 size) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 4 页 - - - - - - - - - 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); else free(str);
9、 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;
10、 GetMemory(str); strcpy(str, hello world); printf(str); 请问运行Test 函数会有什么样的结果?char *GetMemory(void) char p = hello world; return p; void Test(void) char *str = NULL; str = GetMemory(); printf(str); 请问运行 Test 函数会有什么样的结果?名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页
11、,共 4 页 - - - - - - - - - 答:程序崩溃。因为 GetMemory 并不能传递动态内存,Test 函数中的 str一直都是 NULL。strcpy(str, hello world);将使程序崩溃。答:可能是乱码。因为 GetMemory 返回的是指向“栈内存”的指针,该指针的地址不是 NULL,但其原现的内容已经被清除,新内容不可知。void GetMemory2(char *p, int num) *p = (char *)malloc(num); void Test(void) char *str = NULL; GetMemory(&str, 100); strc
12、py(str, hello); printf(str); 请问运行Test 函数会有什么样的结果?答:(1)能够输出hello(2)内存泄漏void Test(void) char *str = (char *) malloc(100); strcpy(str, “ hello ” ); free(str); if(str != NULL) strcpy(str, “ world ” ); printf(str); 请问运行 Test 函数会有什么样的结果?答:篡改动态内存区的内容,后果难以预料,非常危险。因为 free(str);之后, str成为野指针,if(str != NULL)语句不起作用。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 4 页 - - - - - - - - -