0

I am almost done with speller but when I pass it through valgrind, it says there are some mistakes. More specifically, it says a bunch of bytes are still reachable. I don't know whether my problem is with load or unload and have tried editing both for hours. Would greatly appreciate the help!

// Implements a dictionary's functionality

#include <stdbool.h>
#include <stdio.h>
#include "dictionary.h"
#include <stdlib.h>
#include <stdint.h>
#include <strings.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>

//Size integer
int dictionary_size = 0;

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

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

// Hash table
node *table[N];

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    int y = strlen(word);
    char lword[y + 1];
    for (int i = 0; i < y; i ++)
    {
        lword[i] = tolower(word[i]);
    }
    lword[y] = '\0';
    
    int x = hash(lword);
    node *cursor = table[x];
    
    while (cursor != NULL)
    {
        if (strcasecmp(lword, cursor->word) != 0)
        {
            cursor = cursor->next;
        }
        else
        {
            return true;
        }
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    //Credit goes to yangrq1018 on GithubGist
    unsigned int hash = 0;
    for (int i = 0, n = strlen(word); i < n; i++)
    {
        hash = (hash << 2) ^ word[i];
    }
    return hash % N;
}

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

    //Checking if file can be opened
    if (file == NULL)
    {
        return false;
    }
    
    char buffer[LENGTH + 1];

    //Reading and allocating each word in the dictionary to a place in the hash table
    while (fscanf(file, "%s", buffer) != EOF)
    {
        //Creating memory
        node *n = malloc(sizeof(node));

        //Checking if there's enough space
        if (n == NULL)
        {
            fclose(file);
            return false;
        }
        else
        {
            //Copying word into memory
            strcpy (n->word, buffer);
            
            //Assigning info to n pointer
            n->next = NULL;

            //Hashing the word
            int x = hash(buffer);

            //Insert node into hash table
            if (table[x] == NULL)
            {
                table[x] = n;
            }
            else
            {
                n->next = table[x];
                table[x] = n;
            }
            dictionary_size ++;   
        }
    }
    fclose(file);
    return true;
}

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

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    for (int j = 0; j < N; j ++)
    {
        node *cursor = table[j];
        
        while (table[j] != NULL)
        {
            node *tmp = cursor;
            cursor = cursor->next;
            free(tmp);
        }
        free(cursor);
        
        return true;
    }
    return false;
}

1

The problems are in unload. There are two issues to deal with. I'll point you at them and let you figure out how to resolve them. ;-)

First, the while loop is based on the value in table[j]. The node pointed to by table[j] will get freed in the first pass through the loop. That leaves an invalid address in table[j]. I'm not sure why the loop is terminating, since table[j] isn't set to NULL. Maybe there's another pointer that should be tested for null here????

Next, the for loop only executes once, never more. What happens when a return statement is encountered?

If you can't figure it out, leave a comment describing your issue. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .