对于一个小的字典列表程序运行,但是对于一个大的列表它给出了一个错误

For a small dictionary list program runs, but for a large list it gives an error

我是 C 的新手,在一次作业中,我必须在计算机内存中加载 141,000 个单词的词典。

下面的代码在加载较小的词典时有效(我尝试了最多 700 个单词),但在加载较大的词典时,编译器会出现分段错误。

在 运行 调试器上,当加载 140k 单词字典时,我可以看到调用堆栈在队列中没有下一个函数(主函数)error on 140k word list. But for small dictionary with 700 words call stack has the next function (main) in the queue ready for execution the call stack is ok with small list

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    char inword[1]; // to store word
    FILE *infile = fopen(dictionary, "r"); // open file location is in definations of speller.c
    // check if filepointer worked
    if (infile == NULL)
    {
        fprintf(stderr, "Could not open DICTIONARY");
        return false;
    }
    //loop over each word
    while (fscanf(infile, "%s", inword) != EOF)
    {
        node *temp;
        //save word in node
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            fprintf(stderr, "Could not malloc");
            return false;
        }
        strcpy(n->word, inword);
        n->next = NULL;
        // hash word to obtain hash value
        int hashn = hash (inword);
        // insert the word into hash table
        if (table[hashn] == NULL)
        {
            n->next = NULL;
        }
        else
        {
            n->next = table[hashn];
        }
        table[hashn] = n;
        count++;
    }
    fclose(infile);
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    // TODO
    //  printf("xxThe size of dictionary loaded is : %i\n", count); // for testing
    return count;
}

inword[1] 只够存储一个空字符串。

// char inword[1]; // to store word
char inword[100];

使用宽度限制。积极测试预期结果。

// while (fscanf(infile,"%s",inword) != EOF)
while (fscanf(infile,"%99s",inword) == 1)

我怀疑未发布的 node 可能也需要重做。

多个问题。这是更正后的版本:

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary) {
    node *n;
    char inword[sizeof(n->word)]; // <-- use a larger array, assuming n->word is an array
    FILE *infile = fopen(dictionary, "r");
    // check if filepointer worked
    if (infile == NULL) {
        fprintf(stderr, "Could not open DICTIONARY\n");  // <-- add trailing newline
        return false;
    }
    //loop over each word
    while (fscanf(infile, "%99s", inword) == 1) {  // <-- specify maximum length and test for success
        //node *temp;    // <-- unused variable
        //save word in node
        n = malloc(sizeof(node));
        if (n == NULL) {
            fprintf(stderr, "Could not malloc\n");  // <-- add trailing newline
            fclose(infile);  // <-- close input file
            return false;
        }
        strcpy(n->word, inword);
        // hash word to obtain hash value
        int hashn = hash(inword);
        // insert the word into hash table
        n->next = table[hashn];  // <-- works too if table[hashn] is null
        table[hashn] = n;
        count++;
    }
    fclose(infile);
    return true;
}