The address of q
is returned to main
, where there is an attempt to preserve and use the strings. The result can be disastrous.
Consider this foo
function version
char *foo(char *p) {
char *q = (char *)malloc(strlen(p)+1);
strcpy(q, p);
printf("From foo: the string is %s\n", q);
return q;
}
Is the output right? What's the difference against the no-free.c?
Source Code: no-free2.c