Is it safe to return the pointer to a compound literal from a function?
I know the local variables of a function get deallocated after the function returns and that malloc
'd memory is an exception to that. However, the compiler isn't giving any warning when I try to return the pointer to a compound literal. Does this mean it is safe to do so?
I compiled using gcc -Wall -o test test.c
The following code compiled without any warnings and outputs 20
#include <stdio.h>
struct test {
int item;
struct test *next;
} struct_1 = { .item = 10, .next=NULL };
struct test * returnStruct(){
struct test * structPointer = (& ((struct test) { .item = 20, .next = NULL }));
return(structPointer);
}
int main(){
struct_1.next = returnStruct();
printf("%d", (struct_1.next)->item);
return(0);
}
but this code causes a warning
#include <stdio.h>
int * testr(){
int i = 568;
return(&i);
}
int main(){
printf("%d", *testr());
return(0);
}
warning: address of stack memory associated with local variable 'i' returned [-Wreturn-stack-address]
return(&i);
^
1 warning generated.
Also, if compound literals don't get deallocated after the function returns, does that mean I need to call free
on all compound literals in a function?