0

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;

}

1 Answer 1

1

Suppose you have a (non-recursive) function that calls a different function.

bool larger (int a, int b) { return a > b; }

What's the difference between doing

bool check (int a, int b) {

larger(a,b);

printf("%d\n", a); }

or doing

bool check (int a, int b) {

  return larger(a,b);  
printf("%d\n", a); }

Which one will print something of the two functions? Now see if you can infer from this example what would happen if you add return to your recursive function

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .