我试图释放一个Trie,但是在运行valgrind之后,我仍然可以看到很多已用内存。有人可以指出我正确的方向吗?我可以改变什么?我试图将其绘制在纸上,从逻辑上讲,这对我来说很有意义,但显然它不能正常工作。谢谢您的投入!
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
答案 0 :(得分:2)
我想应该是
if (tmp -> children[j] != NULL)
{
destroy(tmp -> children[j]);
}
因为您正在尝试检查NULL条件。
您应该将其更改为void函数,以便可以自由进行递归