Because in function f, is it likely that the buffer reference is lost? I know the function is destroyed after it is called, but if the memory address remains the same, why is that content lost? Another detail, because this is an indefinite behavior. In function g, when using strcpy, the content is not lost. I want to understand what the compiler does, wanted to sample it in assembly. Thankful.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *f(void) {
char buffer[7] = "blabla";
char *ptr = buffer;
return ptr;
}
char *g(void) {
char *pt = malloc(20);
char str[7] = "blabla";
strcpy(pt,str);
return pt;
}
void main(void) {
char *s1 = f();
puts(s1);
char *s2 = g();
puts(s2);
}