I am trying to free a trie, but I can still see a lot of used memory after running valgrind. Could someone point me in the right direction? What could I change? I tried to draw it on paper and logically it makes sense to me, but obviously it doesn´t work properly. Thank you for any inputs!
bool destroy(node *tmp)
{
// Going through all the children nodes
for (int i = 0, number = 0; i < N; i++)
{
// If children node is not NULL, destroy it (recursion)
if (tmp->children[i] != 0)
{
return destroy(tmp->children[i]);
}
}
// At this point all the children nodes should be NULL
// Free current node
free(tmp);
return true;
}
valgrind output:
==5374== HEAP SUMMARY:
==5374== in use at exit: 3,808 bytes in 17 blocks
==5374== total heap usage: 23 allocs, 6 frees, 14,352 bytes allocated
I supppose it should be
if (tmp -> children[j] != NULL)
{
destroy(tmp -> children[j]);
}
For you are trying to check for NULL condition.
You should change it to a void function so that recursion can occur freely