首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CS50 pset5 Speller [2021] -“:(程序没有内存错误”)

CS50 pset5 Speller [2021] -“:(程序没有内存错误”)
EN

Stack Overflow用户
提问于 2021-05-09 15:45:41
回答 1查看 879关注 0票数 0

“我得到了错误":(程序没有内存错误,验证测试失败;有关更多信息,请参见日志。

这是我的代码:

代码语言:javascript
复制
    // Implements a dictionary's functionality
    
    #include <stdbool.h>
    #include <stdio.h>
    #include "dictionary.h"
    #include <ctype.h>
    #include <stdlib.h>
    #include <string.h>
    #include <strings.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
    const unsigned int N = 27;
    
    // Hash table
    node *table[N];
    
    int size_n = 0;
    
    
    ```// Returns true if word is in dictionary, else false
    bool check(const char *word)
    {
        //Hash Number
        unsigned int number;
        number = hash(word);
    
        node *n;
    
        n = table[number];
    
        //Check for the word in the linked-list inside the Hash table
        while (n->next != NULL)
        {
            if (strcasecmp(n->word, word) == 0)
            {
                return true;
            }
    
            n = n->next;
        }
    
        //Return false if word not found in Hash table
        return false;
    }
    
    // Hashes word to a number
    unsigned int hash(const char *word)
    {
        unsigned int number = 0;
    
        //If word starts with '
    
        if (word[0] == 44)
        {
            number = 0;
        }
    
        //Assign Hash number alphbetically
    
        else if (toupper(word[0]) >=65 && toupper(word[0])<=90)
        {
            number = (toupper(word[0]) - 64);
        }
    
        return number;
    }
    
    // Loads dictionary into memory, returning true if successful, else false
    bool load(const char *dictionary)
    {
        //Open dictionary
        FILE *F = fopen(dictionary, "r");
    
        //Check if file is opened
        if (F == NULL)
        {
        return false;
        }
    
        //Temporary string to store words
        char temp[LENGTH + 1];
    
        unsigned int number;
    
        //Initialize linked-list pointers to NULL so that last element of every linked-list points to NULL
        node *p = malloc(sizeof(node));
        for (int i = 0; i < N; i++)
        {
            p->next = NULL;
            table[i] = p;
        }
    
        //Loads the dictionary in the Hash Table
        while(fscanf(F, "%s", temp) != EOF)
        {
            printf("%s", temp);
            number = hash(temp);
    
            //Allocate memory for the dictionary word
            node *n = malloc(sizeof(node));
    
            if (n == NULL)
            {
                return false;
            }
    
            //Store dictionary word in the node
            strcpy(n->word,  temp);
    
            //Insert the node between head and first element of linked-list
            n->next = table[number];
            table[number] = n;
    
            //Counts the number of words of the dictionary stored in the memory
            size_n++;
        }
    
        free(p);
        fclose(F);
        return true;
    }
    
    // Returns number of words in dictionary if loaded, else 0 if not yet loaded
    unsigned int size(void)
    {
        return size_n;
    }
    
    
    // Unloads dictionary from memory, returning true if successful, else false
    bool unload(void)
    {
        //Iterates over each head of the Hash table
        for (int i = 0; i < N; i++)
        {
            node *temp = NULL;
            node *dlt = NULL;
    
            temp = table[i];
    
            while (temp->next != NULL)
            {
                dlt = temp;
                temp = temp->next;
                free(dlt);
            }
        }
        return true;
    }

下面是val差制check50中的错误:

导致val研测试失败;有关更多信息,请参见日志。日志

代码语言:javascript
复制
    running valgrind --show-leak-kinds=all --xml=yes --xml-file=/tmp/tmplj9r16i7 -- ./speller substring/dict substring/text...
    checking for output "MISSPELLED WORDS\n\nca\ncats\ncaterpill\ncaterpillars\n\nWORDS MISSPELLED: 4\nWORDS IN DICTIONARY: 2\nWORDS IN TEXT: 6\n"...
    checking that program exited with status 0...
    checking for valgrind errors...
    Invalid read of size 8: (file: dictionary.c, line: 40)
    Invalid read of size 8: (file: dictionary.c, line: 149)

我找不到别的办法解决这个问题了。如果我这样做,我就会犯错。

对我来说,粗心大意的错误并不明显。我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-11 16:48:34

这是不对的

//初始化指向NULL的链接列表指针,以便每个链接列表的最后一个元素指向空节点*p =malloc(sizeof of (节点));for (int i= 0;i< N;i++) { p->next = NULL;tablei =p};

您没有将链接的列表指针初始化为NULL,而是将它们初始化为p,然后在加载结束时调用空闲的p。这使得每个链接列表都处于无效状态,因为头部指针将指向您不再拥有的内存。

代码语言:javascript
复制
table[i] = NULL;

应该是正确的,尽管仍然没有必要,因为表是一个全局变量,因此默认初始化为零。

正如已经指出的,这一部分检查也是不正确的。

代码语言:javascript
复制
while (n->next != NULL)

检查next是否为NULL总是错误的。我们不在乎next是否为NULL,而只关心n为NULL。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67459582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档