0

enter preformatted text hereSpeller works but valgrind says I'm having memory leaks:

52 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==1454== at 0x4029F6F: malloc (vg_replace_malloc.c:270) ==1454== by 0x804921B: load (dictionary.c:85) ==1454== by 0x8048811: main (speller.c:43) ==1454== ==1454== LEAK SUMMARY: ==1454== definitely lost: 52 bytes in 1 blocks ==1454== indirectly lost: 0 bytes in 0 blocks ==1454== possibly lost: 0 bytes in 0 blocks ==1454== still reachable: 0 bytes in 0 blocks ==1454== suppressed: 0 bytes in 0 blocks ==1454== ==1454== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Here are the relevant snippets:

node* entry=malloc(sizeof(node)); /dictionary line 85, I malloc a node into which fscanf will read new dictionary entry/

if (entry==NULL)

   return 1;
entry->next=NULL;

while (fscanf(infile, "%s", entry->wordval) != EOF) {

int bucket=entry->wordval[0]-97;
if(hash[bucket]==NULL)
  hash[bucket]=entry;
else
  {
  struct node* newnode;
  newnode=malloc(sizeof(node)); /*I suspect this line where I malloc a new node is wrong, but removing it and just using the previous entry node gives rise to more errors, even after freeing entry node*/
  strcpy(newnode->wordval, entry->wordval);
  newnode->next=hash[bucket];
  hash[bucket]=newnode;
  }     
  count++;
}

bool unload (void) //this part is straight out of the lecture so should work {

for (int i=0; i<26; i++)

{ node* ptr=hash[i]; /hash[i] being the head, with 0<=i<26, representing the alphabet/

   while (ptr->next!=NULL)
   {
   node* predptr=ptr;
   ptr=ptr->next;
   free(predptr);
   }

free (ptr); }

Been at this for weeks now, grateful for any suggestions.

  • Please format properly your answer. – user2173 Dec 6 '14 at 22:43
2

I am also a newbie at programming but hope I can help. I've been stuck with the same problem for some time and just solved with some help.

The 52 bytes is the size of one node not freed which in this case is your entry node.

Instead of using an entry node, declare a temporary string before the while loop. assign the input from fscanf to temporary string. declare the node inside the loop and use strcpy to assign the word you stored in temporary string to your new node. Hope this works with you.

| improve this answer | |
  • thanks for the suggestion, but I did try that approach prior to my current one, it resulted in a segfault, while at least the current approach works, if leaky. Is this what you had in mind while (fscanf(infile, "%s", buffer) != EOF) { int bucket=atoi(&buffer[0])-97; struct node* newnode; newnode=malloc(sizeof(node)); strcpy(newnode->wordval, buffer); newnode->next=NULL; if(hash[bucket]==NULL) { hash[bucket]=newnode; } else { hash[bucket]=newnode; etc? – ronga Dec 2 '14 at 6:39
  • I am also facing the same problem, the 52 bytes, i fixed it with your idea, may i ask why does this work? – Yat Long Lo Dec 4 '14 at 13:55

You must log in to answer this question.

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