gpt4 book ai didi

c - 如何修复C语言中的 "Conditional jump or move depends on uninitialised value(s)"错误

转载 作者:行者123 更新时间:2023-11-30 19:01:28 24 4
gpt4 key购买 nike

我在尝试用 C 创建一个 trie 数据库来加载字典并检查给定文本是否有拼写错误时,在内存管理方面遇到了困难。代码编译并运行,但 valgrind 返回一个错误,指出我正在触摸未初始化的内存。不过,我认为使用 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 不会返回内存泄漏,因为我认为我通过将所有创建的节点设置为 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)

最佳答案

该错误是因为节点在分配后需要进行初始化,但您没有一个地方可以执行此操作,因此您在某些节点分配中错过了该步骤。

你应该做的是从 //Initialize trie 注释后面的 load 中取出 10 行代码,并将它们放入一个函数中。此代码为节点分配空间并初始化其所有成员。然后,不要重复此代码(甚至调用 malloc),而是调用新函数。当前在任何地方调用 malloc(sizeof(node)) 时,请将该调用替换为对新函数的调用。特别是,head->children[box] = malloc(sizeof(node)); 行将是 head->children[box] = AllocateNode(); .

仅调用malloc,分配节点中的任何字段都不会被初始化,并且children数组中的各种指针可以在其中具有任何值。当您在 destroy 中使用这些未初始化的值时,任何事情都可能发生。在这种情况下,valgrind 会告诉您问题所在。

关于c - 如何修复C语言中的 "Conditional jump or move depends on uninitialised value(s)"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57750252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com