I am currently working on pset5 and having some issues with errors that were identified by valgrind. My dictionary program seems to run fine- it passes the check50 and it is almost as fast as the staff solution. I thought I was finished with this HW until I ran valgrind and discovered many errors, making me think that I am not understanding some fundamental things.
I have pasted one valgrind issue below and the code that is associated it. Any suggestions or help as to what I am missing is much appreciated.
==17838== 280,000 bytes in 5,000 blocks are definitely lost in loss record 4 of 5
==17838== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17838== by 0x4015F3: make_node (dictionary.c:216)
==17838== by 0x401346: load (dictionary.c:95)
==17838== by 0x400A0D: main (speller.c:45)
Code:
//initialize the bucket backbone of the hash table. buckets is 5000
for (int i = 0; i < buckets; i++) (93)
{
hashtable[i] = make_node(); (95)
}
//a function to initialize a new node
struct node* make_node() (214)
{
struct node* temp = malloc(sizeof(node)); (216)
temp->next = NULL; (217)
return temp; (218)
}
//update- my unload fxn is listed below. thanks!
bool unload(void)
{
node* freebird = malloc(sizeof(node));
if (freebird == NULL)
{
return false;
}
for (int i = 0; i < buckets; i++)
{
freebird = hashtable[i];
while (freebird->next != NULL)
{
node* freebird2 = freebird->next;
free(freebird);
freebird = freebird2;
}
}
return true;
}