What is the correct way to free a float ** like below.
e.g. float ** someArray
for(int i = 0; i < numberOfDimensions; i++)
{
somearray[i] = (float *) malloc(numberOfDimensions * sizeof(float));
}
What is the correct way to free a float ** like below.
e.g. float ** someArray
for(int i = 0; i < numberOfDimensions; i++)
{
somearray[i] = (float *) malloc(numberOfDimensions * sizeof(float));
}
If you have malloc'ed another round of memory and assigned it to each float pointer in the original array, then you should free them as well beforehand:
int i;
for (i = 0; i < numberOfDimensions; i++)
free(someArray[i]);
// and free the container array only now
free(someArray);
Go backwards:
for(int i = 0; i < numberOfDimensions; i++)
{
free(somearray[i]);
}
free(somearray);
Well, in this case you simply free(someArray). Now, if you malloc'd more memory and added that to someArray, then you need to walk that array and free each object.
in other words if you:
for(int i=0; i< whatever; ++i) {
someArray[i] = malloc(...
}
Then you need to walk it again and do frees before you free someArray.
for(int i = 0; i < numberOfDimensions; i++)
{
free(somearray[i]);
somearray[i] = NULL;
}
free(somearray);
somearray=NULL;
(somearray[i] = NULL) this line will break the link of each array element and finally (somearray=NULL) will break the link to the array. These two lines will free up the array of dynamic memory and the OS allocate this freed memory to some other process