edit 1
As suggested by Jonathan Leffler I am now not using names starting with underscores and also deleted spaces around ->
.
________________________________________________________________________________
I am getting a segfault when trying to free struct with recursive function.
Here is my struct:
//creating new trie data ctructure
typedef struct dict
{
bool is_word;
struct dict *children[ALPHABET+1];
}
node;
It is used to store dictionary, which is used in spellchecker. In the end of the program I need to free all memory that was allocated.
And here is the function that I've written. It should call itself and free trie piece by piece. However it gives me segfault after calling itself several times.
bool unload(void)
{
// Check if root
if (temp == root)
{
for (int i = 0; i < ALPHABET+1; i++)
{
if (!temp->children[i] && i != ALPHABET)
{
}
else if (!temp->children[i] && i == ALPHABET)
{
free(temp);
return true;
}
else if(temp->children[i])
{
temp = temp->children[i];
unload();
}
}
}
else
{
for (int i = 0; i < ALPHABET+1; i++)
{
if (!temp->children[i] && i != ALPHABET)
{
}
else if (!temp->children[i] && i == ALPHABET)
{
temp1 = temp;
temp->children[i] = temp;
free(temp1);
return true;
}
else if (temp->children[i])
{
temp = temp->children[i];
unload();
}
}
}
return false;
}
Assume that root , temp , temp1 are global. All of them are of struct _dict. And that when the function is called for the first time temp == root.