在尝试用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)
答案 0 :(得分:1)
错误是因为node
在分配后需要初始化,但是您没有位置可以这样做,因此您在某些节点分配中会错过这一步骤。
您应该做的是在load
注释后的// Initialize trie
中提取10行代码,并将其放入函数中。此代码为节点分配空间并初始化其所有成员。然后,不要重复此代码(甚至调用malloc
),而是调用新函数。您当前正在调用malloc(sizeof(node))
的任何地方,都应将该调用替换为对新函数的调用。特别是,head->children[box] = malloc(sizeof(node));
行将是head->children[box] = AllocateNode();
。
仅在那里调用malloc
,就不会初始化分配的节点中的任何字段,并且children
数组中的各种指针可以在其中具有任何值。当您在destroy
中使用这些未初始化的值时,可能会发生任何事情。在这种情况下,valgrind会告诉您问题所在。