CS50拼写器(pset5)中的内存错误

时间:2020-05-06 05:29:50

标签: c memory cs50

我已经成功编写了可以在加载字典时进行拼写检查的代码。为了释放内存,我编写了unload()函数,并且valgrind显示没有内存泄漏。但是提交后,我遇到了一些隐秘的错误,即

Conditional jump or move depends on uninitialised value(s): (file: dictionary.c, line: 137)
Conditional jump or move depends on uninitialised value(s): (file: dictionary.c, line: 143)

下面分别是我的unload()函数,valgrind屏幕截图和Submit50结果。请帮忙。

谢谢。

//卸载功能

bool unload(void)
{
    for (int i = 0; i < N; i++)
    {
        node* temp = table[i];
        node* cursor = temp;
        while (temp != NULL)
        {
            cursor = temp->next;
            free(temp);
            temp = cursor;
        }
        free(cursor);

    }
    return true;
}

// valgrind result

// submit50 result

1 个答案:

答案 0 :(得分:0)

这是因为在某些情况下temp -> next未初始化。

您可以使其成为每个表存储桶中的最后一个节点,指向NULL。