0

I'm am getting this error - program is free of memory errors, valgrind tests failed; see log for more information.

I have tried so many things but I keep getting more errors. Here is my code:

// Implements a dictionary's functionality

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

#include "dictionary.h"

// 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 = 1;

unsigned int numWords = 0;

// Hash table
node *table[N];

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    //initialize index for hashed word
    int hashNum = hash(word);
    
    //set cursor point to same address as hashtable bucket
    node *cursor =  table[hashNum];
    
    //while the cursor doesn't show NULL, seach the dictionary for the word
    while(cursor != NULL)
    {
        //the word is found if the strcasecmp returns the value of true
        if (strcasecmp(cursor->word, word) == 0)
        {
            return true;
        }
        //if the word has not been found then the cursor will advance
        cursor = cursor->next;
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    // TODO
    char letter = toupper(word[0]);
    return ((int) letter) - ((int) 'A');
}

int word_count = 0;

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    // TODO
    //open the dictionary
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        return false;
    }
    
    //scan the dictionary word by word
    char str[LENGTH + 1];
    while (fscanf(file, "%s", str) != EOF)
    {
        node *newNode = malloc(sizeof(node));
        //checks if malloc succeeded; if it is not succeeded then return false
        if (newNode == NULL)
        {
            return false;
        }
        
        //initializes and calculates index of the word for insertion into hashtable
        int hashNum = hash(str);
        
        //copy word into node if malloc succeeds
        strncpy(newNode->word, str, sizeof(str));
        
        //initialize to point the head to hashtable of index
        //node *head = hashtable[h];
        
        //insert the new node at the beginning of the lists
        if (table[hashNum] == NULL)
        {
            newNode->next = NULL;
        }
        else
        {
            newNode->next = table[hashNum];
        }
        table[hashNum] = newNode;
        numWords += 1;
        
    }
    fclose(file);
    return true;
}

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

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    // TODO
    for (int i = 0; i < N; i++)
    {
        if (table[i] != NULL)
        {
            free(table[i]);
        }
    }
    return true;
}

Here is the log with the error message. It says it's coming from line 76:

running valgrind --show-leak-kinds=all --xml=yes --xml-file=/tmp/tmpruc87cd3 -- ./speller substring/dict substring/text... 
checking for output "MISSPELLED WORDS\n\nca\ncats\ncaterpill\ncaterpillars\n\nWORDS MISSPELLED: 4\nWORDS IN DICTIONARY: 2\nWORDS IN TEXT: 6\n"... 
checking that program exited with status 0... 
checking for valgrind errors... 
112 bytes in 2 blocks are still reachable in loss record 1 of 1: (file: dictionary.c, line: 76) 

Here is line 76:

node *newNode = malloc(sizeof(node));

This is the only error that I'm getting. What am I doing wrong?

1 Answer 1

0
if (table[i] != NULL)
{
    free(table[i]);
}

What you are doing here basically is checking if the table is empty, then empty it if not, however, the table[i] is just the first node of the linked list, you need to go to the next pointer each time before emptying the first one, until the end, to empty them all, here you're basically removing the first node and losing all reference to it.

while (table[i] != NULL)
{
    node *tmp = table[i];
    table[i] = table[i]->next;
    free(tmp);
}

side-note : I don't know how it worked, but N number of buckets should be 26, since you're hashing on the first letter.

You must log in to answer this question.

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