0

I'm sooo close!! My code is running and printing out misspelled words but stopped at message "could not unload dictionary" (I tried on both large and small). However when I run Valgrind, it's reporting a successful memory freed without any leakage.

Here are the screen shots of Valgrind enter image description here
enter image description here

below is my codes for unload

// recursive function for freeing children memory

void del_trie(node* ptr)

{

for(int i = 0; i < ALPHABETS; i++)
    if(ptr->children[i] != NULL)
        del_trie(ptr->children[i]);

free(ptr);

return;

}

/** * Unloads dictionary from memory. Returns true if successful else false. */

bool unload(void)

{

node* current = root;

del_trie(current);

if(root != NULL);
    return false;

return true;

}

Can someone Plzzz help?? Thanks beforehand.

Edited code:

// recursive function for freeing children memory

void del_trie(node* ptr)

{

for(int i = 0; i < ALPHABETS; i++)
{
    if(ptr->children[i] != NULL)
        del_trie(ptr->children[i]);

    ptr->children[i] = NULL;
}

ptr = NULL;
free(ptr);
return;

}

/** * Unloads dictionary from memory. Returns true if successful else false. */

bool unload(void)

{

node* current = root;

del_trie(current);

root = NULL;
free(root);

if(root != NULL);
    return false;

return true;

}

enter image description here

0

When you call free(ptr), it releases the memory pointed to by ptr, but it does not reset the ptr to null. In fact, it doesn't change the contents of the pointer at all. The pointer continues to contain the same address. Thus, your check, if(root != NULL); doesn't work as you expect.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • I think I'm hopeless at this. The changes I've made still not working, now with valgrind still have 1 block floating around (even though in my load code I've closed dictionary file at the end). @Cliff B - Can you give me more hint? And, how do we rerun Valgrind btw? as per the advised "To see them, rerun with: --leak-check=full --show-leak-kinds=all". – MTreacy Nov 10 '16 at 2:45
  • got around Valgrind through --help. But still working on unload. :-( – MTreacy Nov 10 '16 at 3:19
  • Finally figured it out!! Sorry that it took sooo long for me to get my head around it... (newbie), hehehe,,,, I've now passed check50 and even though still slightly longer than the staff ones (0.14 vs 0.06), will give it a try anyway. Thanks again... – MTreacy Nov 12 '16 at 9:13

You must log in to answer this question.

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