Hello! I have a question in regards to malloc/calloc and free in C. What I'm unsure about is the "depth" (for lack of a better word) that free operates to. I know that sounds strange so let this example hopefully explain what I mean:
#include <stdlib.h>
int main ()
{
int length = 10;
char **list = (char **) malloc(sizeof(char *) * length);
char *temp;
int i;
for (i = 0; i < length; i++)
{
temp = (char *) malloc(13);
temp = "Hello world!\0";
list[i] = temp;
}
free(list); // free the char** AND all of its indices' malloc'd memory?
}
So I was wondering if the last call of free would not only free up the char ** memory of "list", but also all the malloc'd memory of each of its indices? Or does it not, and should in fact be called via:
for(i = 0; i < length; i++)
free(list[i]);
free(list);
... which would explicitly free each index before the pointer to char pointers.
So would some compilers know that only freeing the list should free all its indices, or would I always get a memory leak with that? Either way, I want to know if there's an ANSI standard on this... which is probably the second method here if anything.
Thanks in advance for clearing up my confusion!