简体   繁体   English

CS50 pset5 Speller - :( 程序没有内存错误 valgrind 测试失败;有关更多信息,请参阅日志

[英]CS50 pset5 Speller - :( program is free of memory errors valgrind tests failed; see log for more information

I get:我得到:

112 bytes in 2 blocks are still reachable in loss record 1 of 1: 
(file: dictionary.c, line: 112)
line: 112:  node *n = malloc(sizeof(node));

This is my code:这是我的代码:

bool load(const char *dictionary)
{
    FILE *f = fopen(dictionary, "r");
    
    if (f == NULL)
    {
        unload();
        return 1;
    
    char word [LENGTH + 1];
    int coun = 0;

    while (fscanf(f, "%s", word) != EOF)
    {
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            unload();
            return 0;
        }

        strcpy(n->word, word);
        n->next = NULL;
        coun ++;
        
        int index = hash(n -> word);
        n -> next = table[index];
        table[index] = n;
    }
    words_size = coun;
    fclose(f);
    return 1;
}

Any help would be appreciated.任何帮助,将不胜感激。

Here's something u should fix:-这是您应该解决的问题:-

load returns a bool not int , as in line 7, 18 and 30 you are returning 1 which is an int value. load返回一个bool而不是int ,如在第 7、18 和 30 行中,您将返回1 ,它是一个int值。 you should instead do return false or true as per the situation.您应该根据情况return falsetrue try this, if it doesnt work, leave a comments of your error.试试这个,如果它不起作用,请留下您的错误评论。 Here to help!来帮忙! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM