vendredi 29 septembre 2023

Having problem with this C code while running?

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void 
f1(void) {
char *src = "f1";
char *dst;
assert(dst != NULL);
strcpy(dst, src);
printf("%s\n", dst);
free(dst);

}

void 
f2(void) {
char *src = "f2";
char *dst = malloc(strlen(src));
assert(dst != NULL);
strcpy(dst, src);
printf("%s\n", dst);
free(dst); 

}

void 
f3(void) {
char *src = "f3";
char *dst = malloc(strlen(src) + 1);
assert(dst != NULL);
printf("%s\n", dst);
free(dst);

}

void 
f4(void) {
char *src = "f4";
char *dst = malloc(strlen(src) + 1);
assert(dst != NULL);
strcpy(dst, src);
printf("%s\n", dst);

}

void 
f5(void) {
char *src = "f5";
char *dst = malloc(strlen(src) + 1);
assert(dst != NULL);
strcpy(dst, src);
free(dst);
printf("%s\n", dst);

}

static void 
myprintf(char *string) {
printf("%s\n", string);
free(string);

}

void 
f6(void) {
char *src = "f6";
char *dst = malloc(strlen(src) + 1);
assert(dst != NULL);
strcpy(dst, src);
myprintf(dst);
free(dst);

}

void 
f7(void) {
char *src = "f7";
char *dst = malloc(strlen(src) + 1);
assert(dst != NULL);
strcpy(dst, src);
printf("%s\n", dst);
free(src);
free(dst);

}

void 
f8(void) {
char *src = "f8";
char *dst = malloc(strlen(src) + 1);
strcpy(dst, src);
printf("%s\n", dst);
free(dst);

}

int 
main(void) {
f1();
f2();
f3();
f4();
f5();
f6();
f7();
f8();

return 0;
}
While the code can compile, each function contains a common memory mistake that results in             either a run-time error or  undefined behaviour .

main.cpp: In function ‘void f1()’: main.cpp:14:17: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] 14 | char src = "f1"; | ^~~~ main.cpp: In function ‘void f2()’: main.cpp:26:17: warning: ISO C++ forbids converting a string constant to ‘char’ [-Wwrite-strings] 26 | char src = "f2"; | ^~~~ main.cpp:27:23: error: invalid conversion from ‘void’ to ‘char*’ [-fpermissive] 27 | char dst = malloc(strlen(src) + 1); // Allocate enough memory for src and null terminator | ~~~~~~^~~~~~~~~~~~~~~~~ | | | void main.cpp: In function ‘void f3()’: main.cpp:38:17: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] 38 | char src = "f3"; | ^~~~ main.cpp:39:23: error: invalid conversion from ‘void’ to ‘char*’ [-fpermissive] 39 | char dst = malloc(strlen(src) + 1); // Allocate memory for dst | ~~~~~~^~~~~~~~~~~~~~~~~ | | | void main.cpp: In function ‘void f4()’: main.cpp:49:17: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] 49 | char src = "f4"; | ^~~~ main.cpp:50:23: error: invalid conversion from ‘void’ to ‘char*’ [-fpermissive] 50 | char dst = malloc(strlen(src) + 1); // Allocate memory for dst | ~~~~~~^~~~~~~~~~~~~~~~~ | | | void main.cpp: In function ‘void f5()’: main.cpp:61:17: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] 61 | char src = "f5"; | ^~~~ main.cpp:62:23: error: invalid conversion from ‘void’ to ‘char*’ [-fpermissive] 62 | char dst = malloc(strlen(src) + 1); // Allocate memory for dst | ~~~~~~^~~~~~~~~~~~~~~~~ | | | void main.cpp: In function ‘void f6()’: main.cpp:78:17: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] 78 | char src = "f6"; | ^~~~ main.cpp:79:23: error: invalid conversion from ‘void’ to ‘char*’ [-fpermissive] 79 | char dst = malloc(strlen(src) + 1); // Allocate memory for dst | ~~~~~~^~~~~~~~~~~~~~~~~ | | | void main.cpp: In function ‘void f7()’: main.cpp:90:17: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] 90 | char src = "f7"; | ^~~~ main.cpp:91:23: error: invalid conversion from ‘void’ to ‘char*’ [-fpermissive] 91 | char dst = malloc(strlen(src) + 1); // Allocate memory for dst | ~~~~~~^~~~~~~~~~~~~~~~~ | | | void main.cpp: In function ‘void f8()’: main.cpp:102:17: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] 102 | char src = "f8"; | ^~~~ main.cpp:103:23: error: invalid conversion from ‘void’ to ‘char*’ [-fpermissive] 103 | char dst = malloc(strlen(src) + 1); // Allocate memory for dst | ~~~~~~^~~~~~~~~~~~~~~~~ | | | void

Aucun commentaire:

Enregistrer un commentaire