gpt4 book ai didi

c - 在树结构中加载字典并卸载它的问题 - C 编程

转载 作者:行者123 更新时间:2023-11-30 17:08:20 25 4
gpt4 key购买 nike

我仍在尝试调试我的加载函数,该函数应该加载字典文件。我一次又一次地重写我的代码,但没有结果......我知道我们需要有具体的问题,但现在我有很多问题。因此,我将阐述我认为错误的根源。我认为我没有正确初始化我的两个指针,通过初始化,我的意思是分配内存并将它们定义为特定类型的值。

这里,我如何定义和初始化节点和指针的结构:

#include "dictionary.h"
#define NB_NODES 27

// define global structure and pointers
typedef struct node
{
bool is_word;
struct node* children[NB_NODES];
} node;

// initialize pointers and variables
node* root = NULL;
node* current = NULL;
int word_counter = 0;

现在,这是我的 LOAD 实现:

bool load(const char* dictionary)
{
// open dictionary file
FILE* inptr = fopen(dictionary, "r");

if (inptr == NULL)
{
printf("fail to open dictionary\n");
return false;
}

// initialize tools
root = malloc(sizeof(node));
word_counter = 0;
int index = 0;
current = root;

// looks for word until end reached
for (int c = fgetc(inptr); c != EOF; c = fgetc(inptr))
{
// looking words one by one
if (c != '\n')
{
if (c == '\'')
{
index = 26;

if (current->children[index] == NULL)
{
current->children[index] = calloc(1, sizeof(node));

// test
if (current->children[index] == NULL)
{
printf("error with apostrophe");
return 1;
}

}
}

else
{
index = c - 'a';

if (current->children[index] == NULL)
{
current->children[index] = calloc(1, sizeof(node));

// test
if (current->children[index] == NULL)
{
printf("error with characters");
return 1;
}
}
}

// update current pointer to the next node
current = current->children[index];
}

else if (c == '\n')
{
// new word found
if (current->is_word == false)
{
current->is_word = true;
word_counter++;
current = root;
}

// duplicate word
else
{
current = root;
}
}

// character not found
else
{
printf("character not found");
return 2;
}
}

fclose(inptr);
return true;
}

因此,在我的 headers 语句中,我将指针声明为 NULL,并让所有子函数(全局变量)都可以访问它们。之后,在 LOAD 的顶部,我将节点的内存大小分配给我的指针根。我尝试了多种方法来编写此代码,但共享的方法似乎是最符合逻辑的。

我也对找到完整单词后重新初始化根指针的方式存有疑问。也许在这样做的过程中,我“丢失”了刚刚找到的单词的轨迹?

在这种情况下,我们将非常感谢您继续调试,因为这不是我唯一的问题!

这是我尝试运行拼写器时遇到的主要错误,主函数调用加载:

jharvard@appliance (~/Dropbox/pset5): ./speller                  ~cs50/pset5/dictionaries/small/austinpowers.txt
Could not open /home/cs50/pset5/dictionaries/small/austinpowers.txt.
*** Error in `./speller': double free or corruption (!prev): 0x094302d8 ***
Aborted (core dumped)
jharvard@appliance (~/Dropbox/pset5):

我还在 gdb 中启动了拼写器并收到了此错误:

jharvard@appliance (~/Dropbox/pset5): gdb ./speller
Reading symbols from ./speller...done.
(gdb) run ~cs50/pset5/dictionaries/small/austinpowers.txt
Starting program: /home/jharvard/Dropbox/pset5/speller ~cs50/pset5/dictionaries/small/austinpowers.txt
Could not open /home/cs50/pset5/dictionaries/small/austinpowers.txt.
*** Error in `/home/jharvard/Dropbox/pset5/speller': double free or corruption (!prev): 0x0804c2d8 ***

Program received signal SIGABRT, Aborted.
0xb7fdd428 in __kernel_vsyscall ()
(gdb)

奇怪的是,当我在 gdb 中一步步前进时,我可以完成加载循环而不会收到失败......

这是我的 UNLOAD 代码(已修订):

bool unload(void)
{
// recursive_free prototype
void recursive_free (node* node_to_free);

recursive_free(root);

if (root == NULL)
{
return true;
}

else return false;
}


void recursive_free (node* node_to_free)
{
for (int i = 0; i < NB_NODES; i++)
{
// node created to assign memory to free if children found
node* temp;

// if children found
if (node_to_free->children[i])
{
temp = node_to_free;
node_to_free = node_to_free->children[i];
free(temp);
recursive_free(node_to_free);
}

else free(node_to_free);
}
}

如果需要更多的东西来帮助我,尽管问,我会添加。谢谢你们的时光。

最佳答案

您的加载函数看起来不错。在卸载函数中,您必须首先下降所有节点,然后释放底部节点,然后向上一级等等:

void recursive_free (node* node_to_free)
{
for (int i = 0; i < NB_NODES; i++)
{
// if children found
if (node_to_free->children[i])
{
recursive_free(node_to_free->children[i]);
}
}
free(node_to_free);
}

关于c - 在树结构中加载字典并卸载它的问题 - C 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33708482/

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