0

I see that valgrind is saying that the leak is happening at malloc but I am not sure how to free it seeing where it is allocated (within loop). Please help.

'''

// Implements a dictionary's functionality

#include <stdbool.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.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 = (LENGTH + 1) * 122;

//word counter
int num_fword = 0;

// Hash table
node *table[N];

// Returns true if word is in dictionary else false

bool check(const char *word)
{
    //line used to store hash index results
    int h_dex = hash(word);

    //line used to store table
    node *holder = table[h_dex];

    //loop used to check if word in dictionary
    while(holder != NULL)
    {
        //bool used to copy compare word with dictionary
        if(strcasecmp(holder->word, word) == 0)
        {
            return true;
        }

        //line used to move to next node pointer
        holder = holder->next;
    }
    return false;
}

// Hashes word to a number

unsigned int hash(const char *word)
{
    //line used to store lenght of word
    int len = strlen(word);

    //initialization of sum var
    int sum_ofL = 0;

    //loop used to sum up each char in word
    for (int wor = 0; wor < len; wor++)
    {
        //line used to sum up all chars in word and convert word to lowercase
        sum_ofL += tolower(word[wor]);
    }

    //line used to ensure sum is within boundery
    int total_fourl = sum_ofL % N;


    return total_fourl;
}

// Loads dictionary into memory, returning true if successful else false

bool load(const char *dictionary)
{
    //line used to open dictionary file
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        return false;
    }

    //initialization of array
    char word[LENGTH + 1];

    //loop usewd to scar load words from file to node
    while(fscanf(file, "%s", word) != EOF)
    {
        //initialization of new node
        node *nn = malloc(sizeof(node));
        if(nn == NULL)
        {
            unload();
            return false;
        }

        //lines used to copy word to node
        strcpy(nn->word, word);
        nn->next = NULL;

        //line used to store hash index
        int dex = hash(nn->word);

        //line used to intialize head of list
        node *start_list = table[dex];

        //bool used to check head of list is empty
        if (start_list == NULL)
        {
            //line used to store node in list
            table[dex] = nn;
        }
        else
        {
            //line used to move to line in link list
            nn->next = table[dex];
            table[dex] = nn;
        }
        //line used to count words
        num_fword += 1;

    }

    // TODO
    fclose(file);
    return true;
}

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

// Unloads dictionary from memory, returning true if successful else false

bool unload(void)
{
    //loop used to free list
    for(int i = 0; i > N; i++)
    {
        //initialization of nodes
        node *list = table[i];
        node *holder = list;
        node *temp = list;

        //loop used to cycle through list and free memory
        while (holder != NULL)
        {
            holder = holder->next;
            free(temp);

            temp = holder;
        }
    }

    return true;
}

'''

0

Use debug50; set a break at this line for(int i = 0; i > N; i++) in unload. Step through and watch what happens. Basically nothing. The for loop never executes because.....look at it carefully to find the problem.

1
  • LOL, I am so embarrass. I want to thank you for taking the time to point out such a simple mistake. (">" when it should be "<"). I have been rushing so much to finish so I completely overlooked a simple mistake. Again thank you man for taking the time out to reply. @DinoCoderSaurus – Drey Dec 9 '20 at 17:55

You must log in to answer this question.

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