I'm working on the unload function for pset5 - speller and ran into segmentation errors with the first clear function below (dictionary.c unload function calls the recursive function clear). I changed the code to the second clear function below, converting the while loop to a for loop and the error went away, but I don't understand why. Can anyone take a look and tell me why the errors occurred with the first block?
I pasted my full dictionary.c code below the two clear functions if you need it.
First clear function
bool clear(node* cursor)
{
int index = 0;
while (index < 27)
{
if(cursor->children[index])
{
clear(cursor->children[index]);
}
else
{
index++;
}
}
free(cursor);
return true;
}
Second clear function
bool clear(node* cursor)
{
for (int index = 0; index < 27; index++)
{
if(cursor->children[index])
{
clear(cursor->children[index]);
}
}
free(cursor);
return true;
}