Speller - 卸载 trie - 功能无法正常工作
Speller - unloading a trie - function doesn´t work properly
我正在尝试释放一个 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
我想应该是
if (tmp -> children[j] != NULL)
{
destroy(tmp -> children[j]);
}
因为您正在尝试检查 NULL 条件。
你应该把它改成一个空函数,这样递归就可以自由发生了
我正在尝试释放一个 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
我想应该是
if (tmp -> children[j] != NULL)
{
destroy(tmp -> children[j]);
}
因为您正在尝试检查 NULL 条件。
你应该把它改成一个空函数,这样递归就可以自由发生了