I was solving some simple pointer exercises when i came across the following example:
void deallocate2D(int** array, int nrows) {
/* deallocate each row */
int i;
for(i = 0; i < nrows; i++) {
free(array[i]);
}
/* deallocate array of pointers */
free(array);
}
The array is defined in main as int** array1;
Is this a correct way of deallocating memory of a 2d array or should an int*** array be passed in the function instead?
Is there a way to check that memory has been successfully deallocated?
Yes, that’s correct.
int***is only necessary if you intend to change the value of the pointer, which you don’t need to.There do exist tools which can check your memory allocations/frees – Valgrind should be able to do it.