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

标签 c recursion cs50 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 条件。

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

<小时/>

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

相关文章:

c - 如何配置-fno-strict-aliasing?

c - 如何获得 C 程序的优化 RTL?

java - Mybatis SQL 中的递归导致编译时堆栈溢出

arrays - 在 C 中读取命令行参数并检查它是一个数字

Android NDK/JNI - 对自定义头文件中定义的函数的 undefined reference

c - 静态声明后的非静态声明

algorithm - 递归、内循环和时间复杂度

C++ 模板图形类,递归

CS50 维吉尼亚密码 : Can't fix the problem with the spaces in the plaintext

c - && CS50 的 C 语法