Speller 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, thanks for any help.