CS50 pset5 Speller卸载功能出错

时间:2020-08-19 11:30:48

标签: c cs50

:((程序没有内存错误

valgrind测试失败;使用--log重新运行以获取更多信息。

这是我得到的唯一错误。我不知道问题到底是什么或如何解决。

// Number of buckets in hash table
const unsigned int buckets = 5490;

//Number of words in dictionary
int wordCount = 0;

// Hash table
node *table[buckets];

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", dictionary);
        return false;
    }

    char dictionaryWord[LENGTH + 1];

    while (fscanf(file, "%s", dictionaryWord) != EOF)
    {
        node *newNode = malloc(sizeof(node));

        if (newNode == NULL)
        {
            fprintf(stderr, "Not enough memory available to load %s.\n", dictionary);
            return false;
        }

        strcpy(newNode->word, dictionaryWord);

        int index = hash(dictionaryWord);

        if (table[index] == NULL)
        {
            table[index] = newNode;
        }
        else
        {
            newNode->next = table[index];
            table[index] = newNode;
        }
        wordCount++;
    }
    fclose(file);
    return true;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    //iterate through every 'bucket'
    for (int i = 0; i < buckets; i++)
    {
    node *root = table[i]; // initially points to 1st node

    while(root != NULL)
        {
           node *tmp = root; // tmp points to root
            root = root -> next; // root points to next node
            free(tmp); // tmp frees the prior node
        }
   }

    return true;
}

当我运行valgrind

它说

看起来您正在尝试使用可能没有值的变量?仔细查看Dictionary.c的第129行。

第129行是

while(root != NULL)

在卸载功能中

0 个答案:

没有答案