Old question, but I still have some thoughts though.
char * getarrmal(void)
{
char *str;
str = (char *)malloc(10);
str[0] = 'a';
str[1] = 'b';
str[2] = 'c';
str[3] = '\0';
return str;
}
char * getarrdef(void)
{
char *str = "hello";
return str;
}
char * getarrfix(void)
{
char str[10] = "world";
return str;
}
Three functions. First two will return the string address and the string is stored in heap so that you can continue use it in, for example main() function.
In last function the str is a local variable and the returned str cannot be used.
My question is, when I return in the function which is calling the first two, should I manually free them? It is easy to believe for the malloc case it is true, but I am not sure if it is also the case for char *str = "hello".
If I use getarrdef() and not free its return value, then will I have memory leakage somehow?
malloc(or one of its cousins) to do so, you don't callfree. Simple.