I've got a question. I'm using the unload function from my pset5 speller code as an example. I read somewhere that it is good habit to include a return in front of your call to the recursive function. However, when I did it in my case here, it did not work out. If I put
return liberator(node->children[i]);
, instead of what is below down there, that is
liberator(node->children[i];
I get memory leakage. Please advise.
Full code for reference:
/**
* Unloads dictionary from memory. Returns true if successful else false.
*/
bool unload(void)
{
//function declaration
bool liberator(trienode *node);
//use the liberator function to remove all the children of ROOT
liberator(ROOT);
return true;
}
//function to find the leaf of a branch and next remove the memory
bool liberator(trienode *node)
{
for (int i = 0; i < 27; i++)
{
//iterate through each of the node's children structures
//if children structure pointer is not null (ie it is not a leaf), go one level down
if (node->children[i] != NULL)
{
liberator(node->children[i]);
}
}
//if not, all children pointers are null and we can free the memoery and move up to the calling function(ie 1 level up)
free(node);
node = NULL;
return true;
}