PSET5 (Speller) Valgrind Error: Valgrind tests failed

PSET5 (Speller) Valgrind Error: Valgrind tests failed

我没有通过 Valgrind 测试,无法弄清楚我的代码出了什么问题。正如 Valgrind 测试在 malloc() 行指出的那样,问题似乎出在 load() 函数中。谁能帮我看看?任何指导将不胜感激。谢谢!

这是我的代码:

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>

#include "dictionary.h"

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// TODO: Choose number of buckets in hash table
const unsigned int N = 100;

// Hash table
node *table[N];

int count =0;

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    // TODO
    int i = hash(word);
    node *cursor = table[i];

    if (table[i] == NULL)
    {
        return false;
    }
    else
    {
        while(cursor!= NULL)
        {
            if(strcasecmp(cursor->word, word) == 0)
            {
                return true;
            }

            else
            {
                cursor = cursor->next;
            }
        }
    }

    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    // TODO: Improve this hash function
    int bucket;
    if(word[1] != 0)
    {
        bucket = (((toupper(word[0])-'A') * (toupper(word[1]- 'A')))% 10 + (toupper(word[0])-'A'));
    }

    else
    {
        bucket = (((toupper(word[0])-'A') * (toupper(word[0])-'A'))%10 + (toupper(word[0])-'A'));
    }
    return bucket;

}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    // TODO 1
    //open the dictionary
    FILE *file = fopen(dictionary, "r");

    if(file == NULL)
    {
        printf("Can't load the dictionary\n");
        return false;
    }
    //read string from file one at a time
    char word[LENGTH + 1];

    for (int i=0; i < N; i++)
    {
        table[i] = NULL;
    }

    while(fscanf(file, "%s", word) != EOF)
    {
        node *n = malloc(sizeof(node));
        //create a new node for each word
        if(n == NULL)
        {
             unload();
            return false;
        }
        strcpy(n->word, word);
        n->next = NULL;
        count++;
        char *c = n->word;
        int number = hash(c);
        if (table[number] != NULL)
        {
             //point the new node to the first node existing in the table
            n->next = table[number];
           //point the header to the new node
           table[number] = n;
         }
         else
          {
                //n->next = NULL;
                table[number] = n;
            }

    }
    fclose(file);
    return true;
}

// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
    // TODO
    return count;
    //return 0;
}

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    for (int i = 0; i > N; i++)
    {
        node *cursor = table[i];

        while(cursor != NULL)
        {
            node *tmp = cursor;
            cursor = cursor->next;
            free(tmp);
        }
        free(cursor);
    }
    // TODO
    return true;
}

Valgrind 测试显示如下:

Valgrind tests

c.99 是这一行 -> node *n = malloc(sizeof(node));

问题出在卸载中。它不释放任何节点。仔细审视这一行,它包含错误。

for (int i = 0; i > N; i++)