2022年系统级编程:Solution_week .pdf
《2022年系统级编程:Solution_week .pdf》由会员分享,可在线阅读,更多相关《2022年系统级编程:Solution_week .pdf(4页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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 *)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年系统级编程:Solution_week 2022 系统 编程 Solution_week
限制150内