I've been stuck on a segmentation fault error occurring on my unload()
function. Here's the relevant code:
bool unload(void)
{
if (root != NULL)
{
printf("Address: %p\n", (node *)root) ;
freemem(root) ;
}
return true ;
}
void freemem(node *current)
{
// For each node, check if all children are NULL
for (int i = 0; i<27; i++)
{
if (current->children[i] != NULL)
{
freemem(current->children[i]) ;
}
}
// If all children are pointing to NULL, free current node
free(current) ;
return ;
}
At if (current->children[i] != NULL) is where the segmentation fault occurs, and it occurs at the first iteration of the for loop; i.e., at i=0. Valgrind reports the following for that line.
==29979== Invalid read of size 8
==29979== at 0x401488: freemem (dictionary.c:180)
Also, GDB shows that the address of the variable current
doesn't match that of root
at the point of the segmentation fault.
Any ideas what I'm doing wrong? Thank you!
root
node and the localcurrent
node for theload
andcheck
functions, which I freed before exiting these functions. It was the completely wrong thing to do. Code's working properly now. Thank you!