In the walkthrough video, it is told that we should free all pointers in children (trie). Does that mean that all pointers should be set to null?
My program passes CS50 and valgrind doesn't indicate any leaks. But, by inserting printf's inside the freenode function, it is seen that pointers(to trie nodes) are not reset to null. Thus, root(first trie node) is not set to null.
Earlier, I used root as a condition in unload() to return. If root was null, true would be returned, else false. This caused the program to print that it could not be unloaded.
So how do I reset the pointers to NULL, and, what should be the condition in unload for true or false to be returned?
unload();
bool unload(void)
{
freenode(root);
return true;
}
freenode():
// Frees the specified node
void freenode(node* ptr)
{
// Iterate through children array
for (int i = 0; i < 27; i++)
{
if (ptr -> children[i] !=NULL)
{
freenode(ptr -> children[i]);
}
else
{
continue;
}
}
free(ptr);
// Does not seem to work
ptr = NULL;
return;
}