0

Guys why there are memory leaks in my program please help?

    // Implements a dictionary's functionality

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <unistd.h>
#include <ctype.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 = 26;

// Hash table
node *table[N];

//Static word counter to count words
static int wcounter = 0;

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    //Hashing the word to determine it's index value
    int key = hash(word);

    for (node *tmp = table[key]; tmp != NULL; tmp = tmp->next)
    {
        if (strcasecmp(tmp->word, word) == 0)
        {
            return true;
            break;
        }
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    //Making the word ready to be stored in the Index/table
    int key = 0;

    if (islower(word[0]))
    {
        key = word[0] - 'a';
    }

    else
    {
        key = word[0] - 'A';
    }
    return key % N;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    //Open the file provided in the cmd line arguement.
    FILE *file1 = fopen(dictionary, "r");
    if (file1 == NULL)
    {
        return 1;
    }
    //Storing each individual words into the variable
    char word[LENGTH + 1];
    //Creating the header of the word-list
    node *header = NULL;
    //Setting all table[N] nodes to NULL
    for (int i = 0; i < N; i++)
    {
        table[i] = NULL;
    }
    //scanning words from the file until the end of the file
    int errorcheck;
    while ((errorcheck = fscanf(file1, "%s", word)) != EOF)
    {
        //Storing the word in dynamic memory
        node *ptr2word = malloc(sizeof(node));
        if (ptr2word == NULL)
        {
            printf("Memory Error\n");
            return 1;
        }
        strcpy(ptr2word->word, word);
        ptr2word->next = NULL;
        wcounter++;

        //hashing the word for indexing
        int key = hash(word);

        //Storing the word into hash-table
        if (table[key] == NULL)
        {
            table[key] = ptr2word;
        }
        else
        {
            ptr2word->next = table[key];
            table[key] = ptr2word;
        }
    }
    if (errorcheck == EOF)
    {
        return true;
    }
    return false;
}

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

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

check50:Results for cs50/problems/2020/x/speller generated by check50 v3.0.10 :) dictionary.c, dictionary.h, and Makefile exist :) speller compiles :) handles most basic words properly :) handles min length (1-char) words :) handles max length (45-char) words :) handles words with apostrophes properly :) spell-checking is case-insensitive :) handles substrings properly :( program is free of memory errors valgrind tests failed; rerun with --log for more information.

  • You can try our tools to debug memory leaks and segfaults by following this instruction – stensal May 24 at 22:48
1

Every malloc must come with a free, and every open of file must be closed after.

| improve this answer | |
  • I fclosed the file and that problem solved in check50 – yoyo Ahmed May 23 at 13:03
  • but there is still memory missing here I don't know how Invalid read of size 8: (file: dictionary.c, line: 127) – yoyo Ahmed May 23 at 13:04

You must log in to answer this question.

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