我正在学习C语言并调试代码,以便对选定的文本文件进行拼写判断.我这里的最后一个障碍是我的hashmap的加载函数.它成功地从词典文件中读取给定的单词,将其存储,并将其分配给临时node *,但当我try 创建 struct node *table[] = tmp时,问题出现了;

有什么 idea 吗?此外,我的散列函数工作正常,不是问题所在.谢谢大家.

Edit:看起来好像table->next没有更新到指向单链表中下一项的指针.当我try 做new_word->next = table[index];它不改变地址和停留作为0x00.

bool load(const char *dictionary)
{
    // open up file to read dictionary to
    FILE *dict = fopen(dictionary, "r");
    if (dict == NULL)
    {
        printf("Error opening dictionary file.\n");
        return 1;
    }

    // create variable to store the current word in
    char *current_word = malloc(LENGTH * (sizeof(char)));
    if (current_word == NULL)
    {
        printf("Error in allocating memory.\n");
        return 1;
    }

    // initialize index for finding hash index, and count, to store word count
    int index;
    int count = 0;

    while (fgets(current_word, LENGTH, dict) != NULL)
    {
        //open up a node to store the current new word in, also store current word
        node *new_word = malloc(sizeof(node));
        if (new_word == NULL)
        {
            printf("Error allocating memory.\n");
            return 1;
        }
        // copy word to new world variable, store next pointer in variable as well,
        // set as new head of table listy
        strcpy(new_word->word, current_word);
        index = hash(new_word->word);
        new_word->next = table[index];
        table[index] = new_word;
        free(new_word);

        count++;
    }

推荐答案

这个问题很愚蠢:在将 node 链接到哈希表之后,您用free(new_word)来释放 node .只要删除此呼叫即可.

另请注意这些备注:

  • 不需要分配current_wordarray.只需将其定义为具有自动存储的本地对象.

  • 如有错误,切记关闭文件,避免资源泄漏.

  • 在出错的情况下返回true(即:1)有点令人困惑.更一致的API应该是返回成功读取的字数,并在出错时返回-1.

  • 您应该go 掉存储在current_wordfgets()的尾随换行符.

  • 不要对显而易见的事情发表 comments .

以下是修改后的版本:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LENGTH 80
#define HASH_TABLE_LENGTH 1023

typedef struct node {
    struct node *next;
    char word[LENGTH];
} node;

extern int hash(const char *s); // returns a positive value < HASH_TABLE_LENGTH

node *table[HASH_TABLE_LENGTH];

int load(const char *dictionary)
{
    FILE *dict = fopen(dictionary, "r");
    if (dict == NULL)
    {
        fprintf(stderr, "Cannot open dictionary file %s: %s\n",
                dictionary, strerror(errno));
        return -1;
    }

    char current_word[LENGTH];

    int index;
    int count = 0;

    while (fgets(current_word, LENGTH, dict) != NULL)
    {
        node *new_word = malloc(sizeof(node));
        if (new_word == NULL)
        {
            fprintf(stderr, "Error allocating memory.\n");
            fclose(dict);
            return -1;
        }
        // strip the trailing newline if any
        current_word[strcspn(current_word, "\n")] = '\0';
        strcpy(new_word->word, current_word);
        // link the new word at the head of the hash table slot list
        index = hash(new_word->word);
        new_word->next = table[index];
        table[index] = new_word;
        count++;
    }
    fclose(dict);
    return count;
}

// For illustration, here is the code to free the hash table:
void free_hash_table(void) {
    for (int index = 0; i < HASH_TABLE_LENGTH; i++) {
        while (table[index] != NULL) {
            node *p = table[index];
            table[index] = p->next;
            free(p);
        }
    }
}

C++相关问答推荐

为指针 struct 创建宏

CC crate 示例不会与C函数链接

ISO_C_BINDING,从Fortran调用C

为什么复合文字(C99)的返回会生成更多的汇编代码?

如何创建由符号组成的垂直结果图形?

GCC引发不明确的诊断消息

如何知道我是否从非阻塞套接字读取所有内容

获取每个循环迭代结束时的当前时间

如何在C++中处理按键

接受任何参数的函数指针是否与接受不同参数的函数兼容

为什么我无法访问C语言中的文件

未使用sem_open正确初始化信号量

C语言中的指针和多维数组

关于不同C编译器中的__attribute__支持

令人困惑的返回和 scanf 问题相关

C 语言中 CORDIC 对数的问题

如何向 execl 创建的后台程序提供输入?

全局变量 y0 与 mathlib 冲突,无法编译最小的 C 代码

C23 中是否有 __attribute__((nonnull)) 的等效项?

用于内存布局的size命令(文本、数据、bss)