CS50 拼写器 (pset5) 中的内存错误
Memory errors in CS50 speller (pset5)
我已经成功编写了一个可以通过加载字典进行拼写检查的代码。为了释放内存,我编写了 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
这是因为 temp -> next
在某些情况下未初始化。
您可以将其设为每个 table 存储桶中指向 NULL 的最后一个节点。
我已经成功编写了一个可以通过加载字典进行拼写检查的代码。为了释放内存,我编写了 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
这是因为 temp -> next
在某些情况下未初始化。
您可以将其设为每个 table 存储桶中指向 NULL 的最后一个节点。