如何修复 C 中的 "Conditional jump or move depends on uninitialised value(s)" 错误
How to fix "Conditional jump or move depends on uninitialised value(s)" error in C
我在尝试用 C 语言创建一个 trie 数据库来加载字典并检查给定文本是否有任何拼写错误时,遇到了内存管理问题。代码编译并运行,但 valgrind return 出现错误,提示我正在访问未初始化的内存。但是,我认为使用 malloc 就足够了。
我已经尝试将所有创建的节点设置为 NULL,但它仍然告诉我我没有初始化它们。
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <type.h>
#include "dictionary.h"
#define N 27
// Represents a node in a trie
typedef struct node
{
bool is_word;
struct node *children[N];
}
node;
// Represents a trie
node *root;
// Loads dictionary into memory
bool load(const char *dictionary)
{
SIZE = 0;
// Initialize trie
root = malloc(sizeof(node));
if (root == NULL)
{
return false;
}
root->is_word = false;
for (int i = 0; i < N; i++)
{
root->children[i] = NULL;
}
// Open dictionary
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
unload();
return false;
}
// Buffer for a word
char word[LENGTH + 1];
node *head = root;
// Insert words into trie
while (fscanf(file, "%s", word) != EOF)
{
int i = 0;
head = root;
while (word[i])
{
int box = word[i] - 'a';
if (head->children[box] == NULL)
{
head->children[box] = malloc(sizeof(node));
}
else
{
head = head->children[box];
i++;
}
}
head->is_word = true;
SIZE++;
}
// Close dictionary
fclose(file);
// Indicate success
return true;
}
// Unloads dictionary from memory
bool unload(void)
{
void destroy(node *head);
node *head = root;
destroy(head);
head = NULL;
root = NULL;
return true;
}
// Recursively destroys all nodes from last to first.
void destroy(node *head)
{
for (int i = 0, n = N; i < n; i++)
{
// Checks if the current node points to NULL, and stops the func if it does.
if (head->children[i] == NULL)
{
continue;
}
// Runs this function again if the current node points to another.
destroy(head->children[i]);
head->children[i] = NULL;
head->is_word = false;
}
}
我预计 valgrind 不会 return 没有内存泄漏,因为我认为我通过将所有创建的节点设置为 NULL 正确分配并随后释放了内存。这是我遇到的错误示例:
==1529==
==1529== Conditional jump or move depends on uninitialised value(s)
==1529== at 0x4013A4: destroy (dictionary.c:151)
==1529== by 0x4013C0: destroy (dictionary.c:157)
==1529== by 0x4013C0: destroy (dictionary.c:157)
==1529== by 0x4011CC: unload (dictionary.c:140)
==1529== by 0x400DB9: main (speller.c:154)
==1529== Uninitialised value was created by a heap allocation
==1529== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1529== by 0x401136: load (dictionary.c:70)
==1529== by 0x400914: main (speller.c:41)
==1529==
==1529== Conditional jump or move depends on uninitialised value(s)
==1529== at 0x4013A4: destroy (dictionary.c:151)
==1529== by 0x4013C0: destroy (dictionary.c:157)
==1529== by 0x4011CC: unload (dictionary.c:140)
==1529== by 0x400DB9: main (speller.c:154)
==1529== Uninitialised value was created by a heap allocation
==1529== at 0x4C2FB0F: malloc (in /usr/lib/valgrind./vgpreload_memcheck-amd64-linux.so)
==1529== by 0x401136: load (dictionary.c:70)
==1529== by 0x400914: main (speller.c:41)
==1529==
Killed
如果我没有很好地解释这一点,或者如果答案很明显,我是编程新手,我提前道歉。
//更新
感谢1201ProgramAlarm的建议,我已经成功修正了初始化错误。 Valgrind 现在给我一个错误,表明我有内存泄漏。我将尝试像许多人建议的那样在其他函数之外初始化我的函数,看看是否能解决问题。
// 编辑 2
这是我用来纠正初始化错误的新函数:
node *AllocateNode(void)
{
node *head = root;
head = malloc(sizeof(node));
if (head == NULL)
{
free(head);
return false;
}
head->is_word = false;
for (int i = 0; i < N; i++)
{
head->children[i] = NULL;
}
return head;
}
这是 valgrind 给我的新错误:
==19546==
==19546== HEAP SUMMARY:
==19546== in use at exit: 1,344 bytes in 6 blocks
==19546== total heap usage: 383,133 allocs, 383,127 frees, 81,995,696 bytes allocated
==19546==
==19546== 1,344 (672 direct, 672 indirect) bytes in 3 blocks are definitely lost in loss record 2 of 2
==19546== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19546== by 0x40121F: AllocateNode (dictionary.c:170)
==19546== by 0x40113F: load (dictionary.c:74)
==19546== by 0x400914: main (speller.c:41)
==19546==
==19546== LEAK SUMMARY:
==19546== definitely lost: 672 bytes in 3 blocks
==19546== indirectly lost: 672 bytes in 3 blocks
==19546== possibly lost: 0 bytes in 0 blocks
==19546== still reachable: 0 bytes in 0 blocks
==19546== suppressed: 0 bytes in 0 blocks
==19546==
==19546== For counts of detected and suppressed errors, rerun with: -v
==19546== ERROR SUMMARY: 4541 errors from 8 contexts (suppressed: 0 from 0)
错误是因为 node
在分配后需要初始化,但您没有一个地方可以这样做,所以您在某些节点分配中错过了这一步。
你应该做的是从 // Initialize trie
注释后面的 load
中取出 10 行代码并将它们放在一个函数中。此代码为节点分配 space 并初始化其所有成员。然后,不要重复此代码(甚至调用 malloc
),而是调用您的新函数。您当前正在调用 malloc(sizeof(node))
的任何地方,都将调用替换为对新函数的调用。特别是,head->children[box] = malloc(sizeof(node));
行将是 head->children[box] = AllocateNode();
.
只要在那里调用malloc
,分配节点中的none个字段就被初始化,children
数组中的各种指针可以在其中有任何值.当您在 destroy
中使用这些未初始化的值时,任何事情都可能发生。在这种情况下,valgrind 会告诉您问题所在。
我在尝试用 C 语言创建一个 trie 数据库来加载字典并检查给定文本是否有任何拼写错误时,遇到了内存管理问题。代码编译并运行,但 valgrind return 出现错误,提示我正在访问未初始化的内存。但是,我认为使用 malloc 就足够了。
我已经尝试将所有创建的节点设置为 NULL,但它仍然告诉我我没有初始化它们。
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <type.h>
#include "dictionary.h"
#define N 27
// Represents a node in a trie
typedef struct node
{
bool is_word;
struct node *children[N];
}
node;
// Represents a trie
node *root;
// Loads dictionary into memory
bool load(const char *dictionary)
{
SIZE = 0;
// Initialize trie
root = malloc(sizeof(node));
if (root == NULL)
{
return false;
}
root->is_word = false;
for (int i = 0; i < N; i++)
{
root->children[i] = NULL;
}
// Open dictionary
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
unload();
return false;
}
// Buffer for a word
char word[LENGTH + 1];
node *head = root;
// Insert words into trie
while (fscanf(file, "%s", word) != EOF)
{
int i = 0;
head = root;
while (word[i])
{
int box = word[i] - 'a';
if (head->children[box] == NULL)
{
head->children[box] = malloc(sizeof(node));
}
else
{
head = head->children[box];
i++;
}
}
head->is_word = true;
SIZE++;
}
// Close dictionary
fclose(file);
// Indicate success
return true;
}
// Unloads dictionary from memory
bool unload(void)
{
void destroy(node *head);
node *head = root;
destroy(head);
head = NULL;
root = NULL;
return true;
}
// Recursively destroys all nodes from last to first.
void destroy(node *head)
{
for (int i = 0, n = N; i < n; i++)
{
// Checks if the current node points to NULL, and stops the func if it does.
if (head->children[i] == NULL)
{
continue;
}
// Runs this function again if the current node points to another.
destroy(head->children[i]);
head->children[i] = NULL;
head->is_word = false;
}
}
我预计 valgrind 不会 return 没有内存泄漏,因为我认为我通过将所有创建的节点设置为 NULL 正确分配并随后释放了内存。这是我遇到的错误示例:
==1529==
==1529== Conditional jump or move depends on uninitialised value(s)
==1529== at 0x4013A4: destroy (dictionary.c:151)
==1529== by 0x4013C0: destroy (dictionary.c:157)
==1529== by 0x4013C0: destroy (dictionary.c:157)
==1529== by 0x4011CC: unload (dictionary.c:140)
==1529== by 0x400DB9: main (speller.c:154)
==1529== Uninitialised value was created by a heap allocation
==1529== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1529== by 0x401136: load (dictionary.c:70)
==1529== by 0x400914: main (speller.c:41)
==1529==
==1529== Conditional jump or move depends on uninitialised value(s)
==1529== at 0x4013A4: destroy (dictionary.c:151)
==1529== by 0x4013C0: destroy (dictionary.c:157)
==1529== by 0x4011CC: unload (dictionary.c:140)
==1529== by 0x400DB9: main (speller.c:154)
==1529== Uninitialised value was created by a heap allocation
==1529== at 0x4C2FB0F: malloc (in /usr/lib/valgrind./vgpreload_memcheck-amd64-linux.so)
==1529== by 0x401136: load (dictionary.c:70)
==1529== by 0x400914: main (speller.c:41)
==1529==
Killed
如果我没有很好地解释这一点,或者如果答案很明显,我是编程新手,我提前道歉。
//更新
感谢1201ProgramAlarm的建议,我已经成功修正了初始化错误。 Valgrind 现在给我一个错误,表明我有内存泄漏。我将尝试像许多人建议的那样在其他函数之外初始化我的函数,看看是否能解决问题。
// 编辑 2
这是我用来纠正初始化错误的新函数:
node *AllocateNode(void)
{
node *head = root;
head = malloc(sizeof(node));
if (head == NULL)
{
free(head);
return false;
}
head->is_word = false;
for (int i = 0; i < N; i++)
{
head->children[i] = NULL;
}
return head;
}
这是 valgrind 给我的新错误:
==19546==
==19546== HEAP SUMMARY:
==19546== in use at exit: 1,344 bytes in 6 blocks
==19546== total heap usage: 383,133 allocs, 383,127 frees, 81,995,696 bytes allocated
==19546==
==19546== 1,344 (672 direct, 672 indirect) bytes in 3 blocks are definitely lost in loss record 2 of 2
==19546== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19546== by 0x40121F: AllocateNode (dictionary.c:170)
==19546== by 0x40113F: load (dictionary.c:74)
==19546== by 0x400914: main (speller.c:41)
==19546==
==19546== LEAK SUMMARY:
==19546== definitely lost: 672 bytes in 3 blocks
==19546== indirectly lost: 672 bytes in 3 blocks
==19546== possibly lost: 0 bytes in 0 blocks
==19546== still reachable: 0 bytes in 0 blocks
==19546== suppressed: 0 bytes in 0 blocks
==19546==
==19546== For counts of detected and suppressed errors, rerun with: -v
==19546== ERROR SUMMARY: 4541 errors from 8 contexts (suppressed: 0 from 0)
错误是因为 node
在分配后需要初始化,但您没有一个地方可以这样做,所以您在某些节点分配中错过了这一步。
你应该做的是从 // Initialize trie
注释后面的 load
中取出 10 行代码并将它们放在一个函数中。此代码为节点分配 space 并初始化其所有成员。然后,不要重复此代码(甚至调用 malloc
),而是调用您的新函数。您当前正在调用 malloc(sizeof(node))
的任何地方,都将调用替换为对新函数的调用。特别是,head->children[box] = malloc(sizeof(node));
行将是 head->children[box] = AllocateNode();
.
只要在那里调用malloc
,分配节点中的none个字段就被初始化,children
数组中的各种指针可以在其中有任何值.当您在 destroy
中使用这些未初始化的值时,任何事情都可能发生。在这种情况下,valgrind 会告诉您问题所在。