Correct use of free() when deallocating a 2d matrix in c

  • A+
Category:Languages

I'm just starting to learn coding in c, and I have a few questions regarding 2d matrices in combination with the free() command.

I know that you first need to create an array with pointer, pointing to the different columns of the matrix:

double **array = (double **)malloc(5*sizeof(double *)); for(int n = 0; n<5; n++){  array[n] = (double *) malloc(6*sizeof(double)); 

I know that the correct way to then deallocate this matrix is to first deallocate the individual rows and then the array pointer itself. Something along the lines of:

for (i = 0; i < nX; i++){      free(array[i]); }       free(array); 

My question is: Why is this necessary? I know that this incorrect, but why can't you just use: free(array)? This would deallocate the pointer array, to my understanding. Won't the memory that is used by the columns just be overwritten when something else needs acces to it? Would free(array) lead to corrupted memory in any way?

Any help is much appreciated!

 


Your code, not only allocate memory for array of pointers (the blue array), but in the for loop, you also allocate memory for the red arrays as well. So, free(array) line, alone, will just free the memory allocated by the blue array, but not the red ones. You need to free the red ones, just before loosing the contact with them; that is, before freeing the blue array.

And btw;

Won't the memory that is used by the columns just be overwritten when something else needs acces to it?

No. The operating system will keep track of the memory allocated by your process (program) and will not allow any other process to access the allocated memory until your process terminates. Under normal circumstances —I mean, remembering the C language not having a garbage collector— the OS never knows that you've lost connection with the allocated memory space and will never attempt like, "well, this memory space is not useful for this process anymore; so, let's de-allocate it and serve it for another process."

Correct use of free() when deallocating a 2d matrix in c

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: