'PSET5 (Speller) Valgrind Error: Valgrind tests failed

I failed to pass the Valgrind tests and couldn't figure out what went wrong with my code. It seems like the issue is in the load() function as the Valgrind tests pointed out at the malloc() line. Could anyone help me take a look? Any guidance would be appreciated. Thank you!

Here is my code:

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

Here is what the Valgrind tests show:

Valgrind tests

c.99 is this line -> node *n = malloc(sizeof(node));



Solution 1:[1]

The problem is in unload. It doesn't free any nodes. Review this line carefully and critically, it contains the error.

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DinoCoderSaurus