I have been trying to free memory allocated via malloc() using free().
Some of the structs it does free but leaves some the way they were and they also remain linked to their children. It also never frees the root (gRootPtr) for a binary tree.
I am using Xcode to find out if the memory used by the binary tree has been freed and also use the if statement.
Code I am using to free the memory:
void FreeMemory(InfoDefiner *InfoCarrier)
{
if ((*InfoCarrier) != NULL) {
FreeMemory((&(*InfoCarrier)->left));
FreeMemory((&(*InfoCarrier)->right));
free((*InfoCarrier));
}
}
Code I am using to see if the memory has been freed.
if (gRootPtr != NULL) {
return 1;
}
else{
return 0;
}
First, free does not change the pointer itself.
If you want the pointer to go back to NULL, you must do this yourself.
Second, there are no guarentees about what will happen to the memory pointed to by the pointer after the free:
Note that this means that you cannot actually test if
free()works. Strictly speaking, it’s legal forfree()to be implemented by doing absolutely nothing (although you’ll run out of memory eventually if this is the case, of course).