gpt4 book ai didi

c - Speller - 卸载特里树 - 功能无法正常工作

转载 作者:行者123 更新时间:2023-11-30 18:44:29 25 4
gpt4 key购买 nike

我正在尝试释放一个特里树,但在运行 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 条件。

您应该将其更改为 void 函数,以便可以自由地进行递归

<小时/>

关于c - Speller - 卸载特里树 - 功能无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57842993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com