【问题标题】:Segmentation fault in C when trying to load dictionary into memory (cs50 pset5, speller)尝试将字典加载到内存时 C 中的分段错误(cs50 pset5,拼写器)
【发布时间】:2020-10-02 22:49:12
【问题描述】:

我已经部分完成了 cs50 的 pset5,但是每次我尝试运行我的程序时,由于某种原因,我都会遇到段错误。

在我的load 函数中,我打开字典文件并通过调用init_table() 来初始化我的哈希表。然后,我使用fscanf扫描字典文件,为字典中的每个单词创建一个node *n,并将dict_word中的单词复制到这个节点中。然后我使用我的hash 函数(基于 djb2)来存储该节点单词的索引。

如果table[index] == NULL,那么我将table[index] 和一个名为head 的节点都设置为节点n,并将下一个地址设置为NULL。否则,我将下一个节点设置为table[index],并将table[index] 设置为当前节点n

我还没有释放任何内存,因为这将在另一个名为 unload 的函数中完成,但我怀疑我当前的问题可能是由于不同的原因。任何帮助将不胜感激。

// Implements a dictionary's functionality

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

#include "dictionary.h"

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// Number of buckets in hash table (A-Z)
const unsigned int N = 5381;

// Hash table
node *table[N];

// initialize hash table (set all values to NULL)
// reference video: https://youtu.be/2Ti5yvumFTU
void init_table()
{
    for (int i = 0; i < N; i++)
    {
        table[i] = NULL;
    }
}

// Hashes word to a number
// hash function: djb2
// retrieved from http://www.cse.yorku.ca/~oz/hash.html
unsigned int hash(const char *word)
{
    unsigned int hash_value = N;
    int c;

    while ((c = *word++))
    {
        hash_value = ((hash_value << 5) + hash_value) + c; /* hash * 33 + c */
    }
    return hash_value;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // open dictionary file, check if NULL
    FILE *dict_file = fopen(dictionary, "r");
    if (dict_file == NULL)
    {
        return false;
    }

    init_table();

    // create char for each dictionary word (max length + nul)
    char dict_word[LENGTH + 1];

    // create beginning node that serves as first item in linked list
    node *head;

    // scan until end of file
    while (fscanf(dict_file, "%s", dict_word) != EOF)
    {
            // create a node n for each word and copy string into it
            node *n = malloc(sizeof(node));
            if (n == NULL)
            {
                return false;
            }
            strcpy(n->word, dict_word);

            // hash the word and store as index (which tells which linked list to use)
            int index = hash(n->word);

            // if table[index] is NULL, set it and head as node n, set next node as NULL
            if (table[index] == NULL)
            {
                head = table[index] = n;
                n->next = NULL;
            }
            // otherwise set next node as table[index], table[index] as current node n
            else
            {
                n->next = head;
                table[index] = n;
            }
    }
    return true;
}

编辑:阅读 Boris Lipschitz 的回答后,我意识到了问题所在,并进一步了解了如何使用调试器来解决它。我修改了const unsigned int N = 5381 并将N 的值更改为25 以表示字母表中每个字母的存储桶(尽管稍后我可能会更改此值以优化我的程序)。对于我的hash 函数,就像鲍里斯所说的那样,没有什么可以阻止hash_value 超过N,所以我将return hash_value; 改为return hash_value % N;,这将给我一个正确的输出我的桌子的界限。

【问题讨论】:

  • 你能把整个dictionary.c贴出来让别人编译/运行/调试吗?

标签: c memory segmentation-fault cs50


【解决方案1】:

您的 hash 函数中绝对没有任何内容可以阻止它达到 5381 或更高。然后你用它访问table...繁荣,分段错误。

反向问题:为什么不直接在调试器中运行代码?你会比我们任何人都更快地知道答案。

【讨论】:

  • 谢谢,我会努力解决这个问题。我没有使用调试器的原因是因为我不完全确定如何使用它,并且认为它不能在这里使用。但是,我将研究 GDB 调试器并尝试解决问题,并在我能够弄清楚时标记您的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-21
  • 2020-10-04
  • 2020-08-25
  • 2020-08-08
  • 2022-06-16
相关资源
最近更新 更多