I have completed the code for pset5 but I have a super big memory leak which i believe comes from not pointing each backed in the hash_table correctly. Below I have unload function and valgrind's results. If anyone can help me by pointing what's wrong with the unload function that would be great!! Thanks in advance.
load part that seems to create the problem in line 95:
while (fscanf(fp,"%s", word_c)!=EOF)
{
node *new_node=malloc(sizeof(node));
strcpy(new_node->word,word_c);
int h=hash_function(word_c);
++word_count;
bool unload(void)
{
node *cursor=NULL;
for (int i=0; i<SIZE; i++)
{
cursor=hash_table[i];
while (cursor!=NULL)
{
node *temp=cursor;
cursor=cursor->next;
free(temp);
}
}
return true;
}
Valgrind result:
==5342==
==5342== HEAP SUMMARY:
==5342== in use at exit: 8,011,640 bytes in 143,065 blocks
==5342== total heap usage: 143,093 allocs, 28 frees, 8,014,232 bytes allocated
==5342==
==5342== 8,011,640 bytes in 143,065 blocks are definitely lost in loss record 1 of 1
==5342== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5342== by 0x401469: load (dictionary.c:95)
==5342== by 0x400A9D: main (speller.c:45)
==5342==
==5342== LEAK SUMMARY:
==5342== definitely lost: 8,011,640 bytes in 143,065 blocks
==5342== indirectly lost: 0 bytes in 0 blocks
==5342== possibly lost: 0 bytes in 0 blocks
==5342== still reachable: 0 bytes in 0 blocks
==5342== suppressed: 0 bytes in 0 blocks
==5342==
==5342== For counts of detected and suppressed errors, rerun with: -v
==5342== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)