Supposing I have a 2 dimensional array which was created with something like this,
char **foo = (char **) malloc(height * sizeof(char *));
for(i = 0; i <= height; i++)
foo[i] = (char *) malloc (width * sizeof(char *));
First of all, Is this even the right way to create an array like this?. The catch here is, 'height' and 'width' is something that is set during runtime.
This seems to work, but which is the best strategy to free this 2d array. free(funge) sounds wrong. Going by some other posts in here, I guess I will have free each row one by one?
I did try something like this,
for (height = 0; height < ip_ptr->funge_height; height++) {
free(funge[height]);
}
free(funge)
This, however gives me a double free pointer exception. Does this mean, I don't have to manage this piece of memory?. I was of the impression that, for every malloc'ed memory we should call free().