0

So, I'm finishing up pset5 and I'm now just trying to make sure theres no memory leaks. Unfortunaly there are 244 bytes being lost and I'm not sure why they're being lost. This is my free code:

bool freeStuff(node* spider) //recursive function to free
{
    for(int i = 0; i < 27; i++)
    {
        if(spider -> children[i] != NULL)
   {
            freeStuff(spider -> children[i]);
        }
    }
    free(spider);
    return true;
}
bool unload(void)
{
    return freeStuff(head);
}

Full code: http://pastebin.com/L9e58t9c

2

Your code isn't freeing recursively. It should keep on going down the tree while children[i]!=NULL, only freeing the node when that condition isn't met anymore. It then returns up the tree, freeing each node in turn. In your case, you are going down to the final children[i] then freeing spider, you're not going back up the tree.

3
  • I tried changing my freeStuff method to use while instead of if, but by doing that I get a segmentation fault. – Kypore Aug 15 '16 at 22:46
  • Your original freeStuff() function is good. Just think about whether the line 'return true' belongs there. – ronga Aug 16 '16 at 6:09
  • It occurs to me that the error might lie in your load function. After malloc'ing new children node, you might want to initialize its isword key value to false and children to NULL. You are after all testing if children=NULL in your unload function. – ronga Aug 18 '16 at 12:28

You must log in to answer this question.

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