SSD06 Practical Quiz 2 个人解答

C语言内存错误解析

1.

Are there any memory errors in the following program? If so, identify all of the errors and provide a corrected code fragment to alleviate the problem. 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);
        
        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 */
        }
}




















2.

Are there any memory errors in the following program? If so, identify all of the errors and provide a corrected code fragment to alleviate the problem.

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 = malloc(strlen(str) + 1);
        strcat(newstr, str); /* set newstr to str */
        if (strcmp(newstr, "1") == 0) { /* newstr is "1" */
                return 1;
        }
        free(newstr);
        return 0;
}

void main() {
        char *strArr[4] = {"1", "2", "3", "4"};
        int i;
        
        for(i = 0; i < 4; i++) {
                printf("%d\n", checkIf1(strArr[i]));
        }
}









3.

Are there any memory errors in the following program? If so, identify all of the errors and provide a corrected code fragment to alleviate the problem.

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++) {
                results[i] = mergeSingleIfDifferent(strA1[i], strA2[i]);                
        }
}

void printAndFree(int c, char *str) {
        if (str != NULL) {
                printf("%d: %s\n", c, str);
                free(str);
        }
}

void main() {
        char *strArr1[8] = {"1", "2", "3", "4", "5", "6", "7", "8"};
        char *strArr2[8] = {"a", "2", "c", "4", "e", "6", "g", "8"};
        char *results[8];
        int i;
        
        mergeArrayIfDifferent(results, strArr1, strArr2, 8);
        for(i = 0; i < 8; i++) {
                printAndFree(i, results);
        }
}

 

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); 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 */ } } 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 = malloc(strlen(str) + 1); strcpy(newstr, str); /* set newstr to str */ if (strcmp(newstr, "1") == 0) { /* newstr is "1" */ return 1; } free(newstr); return 0; } void main() { char *strArr[4] = {"1", "2", "3", "4"}; int i; for(i = 0; i < 4; i++) { printf("%d\n", checkIf1(strArr[i])); } } 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++) { results[i] = mergeSingleIfDifferent(strA1[i], strA2[i]); } } void printAndFree(int c, char *str) { if (str != NULL) { printf("%d: %s\n", c, str); free(str); } } void main() { char *strArr1[8] = {"1", "2", "3", "4", "5", "6", "7", "8"}; char *strArr2[8] = {"a", "2", "c", "4", "e", "6", "g", "8"}; char *results[8]; int i; mergeArrayIfDifferent(results, strArr1, strArr2, 8); for(i = 0; i < 8; i++) { printAndFree(i, results); } }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值