c - valgrind 显示未释放的内存

标签 c valgrind free

我的代码是:

bool check(const char* word)
{
    char letter;
    node* nodes = malloc(sizeof(node));
    for (int i = 0; isalpha(word[i]) != 0; i++)
    {   
        letter = tolower(word[i]);
        if (i == 0)
        {
            if (root->children[(int)letter - 96] == NULL)
                return false; 
            nodes = root->children[(int)letter - 96];
        }        
        else
        {
            if (nodes->children[(int)letter - 96] == NULL)
            {
               return false;
            }
            nodes = nodes->children[(int)letter - 96];
        }       
    } 
    if (nodes->value == 1)
        return true;
    else
        return false;    
    free (&letter);
    free (nodes->children);
    free (&nodes->value);
    free (nodes);
}

valgrind 说我没有释放第 4 行中创建的变量,但我不明白为什么,因为我在最后释放了它。

最佳答案

这些行

if (nodes->value == 1)
    return true;
else 
    return false;  

确保函数在它可以释放任何内存之前返回。

关于c - valgrind 显示未释放的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33534648/

相关文章:

c - 在c中使用fseek和结构数组

c - 如何在使用 SDL2 的程序中查找 valgrind 检测到的内存错误的原因

c - Valgrind 无效读取大小 1

c调试问题,免费方法

C - 二分搜索树中 ")"之前应为 "*"

c - 用户空间代码以什么顺序执行?

c - const char* 应该在 C 中释放吗?

c - Malloc-释放后使用内存未触发错误

c++ - 为什么编译共享库后函数的名称会改变?

c++ - std::wstring::find() 在 g++ 中损坏?