Old question, but I still have thoughts.
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. The first two return the string address, and the string is stored on the heap, so you can continue to use it, for example, the main () function.
In the last function, str is a local variable, and the str returned cannot be used.
My question is: when I return to the function that calls the first two, do I have to manually release them? It's easy to believe that this is true for the malloc case, but I'm not sure if this also applies to char * str = "hello".
If I use getarrdef () and donβt free its return value, will I have a memory leak somehow?
source
share