簡體   English   中英

CS50 pset5 Speller - :( 程序沒有內存錯誤 valgrind 測試失敗;有關更多信息,請參閱日志

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

我得到:

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));

這是我的代碼:

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;
}

任何幫助,將不勝感激。

這是您應該解決的問題:-

load返回一個bool而不是int ,如在第 7、18 和 30 行中,您將返回1 ,它是一個int值。 您應該根據情況return falsetrue 試試這個,如果它不起作用,請留下您的錯誤評論。 來幫忙! :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM