0

I have been looking at this for about 5 days now and I can´t find where I am leaking memory or if I have any problems with my code logic. Everything compiles and Valgrind output is below. I am thinking the problem could be on the check function.

// Implements a dictionary's functionality

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.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 = 7000;

// Hash table
node *table[N];

//Keep track of word count
int word_count = 0;

//Check if dic was loaded
bool dic_loaded = false;

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    //hash the word
    int index = hash(word);

    //access index
    node *cursor = NULL;
    cursor = table[index];

    // loop indexes and compare words
    while (cursor != NULL)
    {
        if (strcasecmp (cursor -> word, word) == 0)
        {
            return true;

        }
        else
        {
            cursor = cursor -> next;

        }

    }
    return false;

}


// Hashes word to a number
// source: 04 - http://www.cse.yorku.ca/~oz/hash.html loselose

unsigned int hash(const char *word)
{
    unsigned int hash = 0;
    int c;

    while ((c = *word++))
    {
        hash += c;

    }

    return hash;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    char word[LENGTH + 1];
    // open dictionary file and check if null

    FILE *dictionary_file = fopen(dictionary, "r");

    if (dictionary_file == NULL)
    {
        return false;
    }

    // Create temp list

    node *temp = NULL;

    // create loop to scan through all the words from *dictionary_file

    while (fscanf(dictionary_file, "%s", word) != EOF)
    {
        //count each word for size()
        word_count++;

        //Alocate memory for a node

        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            return false;

        }
        //copy string from dictionary into a node
        strcpy (n -> word, word);

        //call hash function to return an index

        int index = hash(word);
        temp = table[index];

        //create list head

        if (temp == NULL)
        {
            table[index] = n;

        }
        else // keeps adding the next node

        {
            n -> next = table[index];
            table[index] = n;

        }

    }
    dic_loaded = true;
    fclose(dictionary_file);
    return true;

}


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

    }
    return false;

}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    //iterate throught the linked list

    for (int i = 0; i < N; i++)
    {
        // set cursor to table index
        node *cursor = table[i];

       while (cursor != NULL)
       {
           node *temp = cursor;   // set temp to table index
           cursor = cursor -> next;  // move cursor
           free(temp);  // free temp
           return true;
       }



   }
   return false;
}´´´
1
  • I still need help! something´s not right.
    – snowboly
    Jun 18 '20 at 21:32
0

There seems to be an issue with your temporary var in load. The code has both tmp and temp. My guess is that you wanted to use the definition and initialization of tmp, but the var name that you used is temp, with an e. Perhaps if you reconcile these two different vars, tmp and temp, you'll resolve this problem.

I would also ask if this is the actual code that you're running? This code has compile errors, one related to tmp being unused and another related to unload.

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

3
  • Thanks!! that solved part of it. Yes there´s more code I´ll edit the original post. Valgrind returned this: ``` MISSPELLED WORDS A Could not unload dictionaries/large. ==328== ==328== HEAP SUMMARY: ==328== in use at exit: 8,013,096 bytes in 143,091 blocks ==328== total heap usage: 143,096 allocs, 5 frees, 8,023,416 bytes allocated ==328== ==328== 8,013,096 bytes in 143,091 blocks are still reachable in loss record 1 of 1
    – snowboly
    Jun 18 '20 at 13:47
  • I will take another look at my unload function. But I have less errors now thanks!
    – snowboly
    Jun 18 '20 at 13:49
  • I think I´ve fixed the iteration in unload but still getting memory leak and few other errors: :( handles most basic words properly expected "MISSPELLED WOR...", not "MISSPELLED WOR..." :( spell-checking is case-insensitive expected "MISSPELLED WOR...", not "MISSPELLED WOR..." :( program is free of memory errors valgrind tests failed; rerun with --log for more information.
    – snowboly
    Jun 18 '20 at 15:38

You must log in to answer this question.

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