0

The programme compiles fine but when i run help50 for leaks i get the following, though i have freed the children. i appreciate the help:

==11548== For counts of detected and suppressed errors, rerun with: -v
==11548== ERROR SUMMARY: 29 errors from 4 contexts (suppressed: 0 from 0)

Helping with...                                                                  

==11548== 82,217,184 (107,744 direct, 82,109,440 indirect) bytes in 481 
blocks are definitely lost in loss record 3 of 3
==11548==    at 0x4C2AB80: malloc (in 
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==11548==    by 0x401053: gnew_node (dictionary.c:32)
    ==11548==    by 0x4011C3: load (dictionary.c:82)
==11548==    by 0x4008CD: main (speller.c:40)

Looks like your program leaked 82,217,184 bytes of memory. Did you forget to 

free memory that you allocated via malloc? Take a closer look 
at line 32 of dictionary.c.

my gnew_node is:

   struct node*gnew_node(void)
     {

  children=(struct node*)malloc(sizeof(struct node));
   if(children)

   children->is_word=false;
   for(int i=0;i<27;i++)

   children->next_char[i]=NULL;

   return children;

}

this is how i used it in load:

 if(!trav->next_char[INDEX])
        {
        trav->next_char[INDEX]=gnew_node();
        }
         trav=trav->next_char[INDEX];
       }

my unload:

bool unload(void)
{
// TODO
 struct node* Pnode;

for(int k=0;k<27;k++)
  {
      Pnode=children;
      if(Pnode->next_char[k]!=NULL) 
      free(children->next_char[k]);
  }
  free(children);

  for(int k=0;k<27;k++)
   {
      Pnode=root_ptr;
     if(Pnode->next_char[k]!=NULL)
      free(Pnode->next_char[k]);
     }

    free(root_ptr);
    return true;
}

this my full code https://pastebin.com/XicAwNuK

1 Answer 1

0

This unload() function is not recursive and does not traverse the trie. At best, it will only free the memory allocated to the root node and the first generation children. With the large dictionary, most of the trie will be lost instead of being freed.

Hint: If you were intending to make it recursive, perhaps you meant to call unload in some of the places where you called free?

Also, I don't understand the reason for processing children and root. There isn't enough code to understand what they are. My next question would be Why are there (apparently) two tries? If there aren't two tries, then why are there two globals pointing to the same memory space, or are they? As I said, not enough code to know what's going on.

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

1
  • i have two malloc to free.i have posted a pastebin of full code.please @Cliff B try to be abit clear i dont understand on the recursive point. Dec 8, 2017 at 12:30

You must log in to answer this question.

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