0

I checked50 and this showed up

program is free of memory errors valgrind tests failed; rerun with --log for more information.

What did I do wrong? Valgrind showed no error...

#include <stdbool.h>
#include <stdlib.h>
#include <cs50.h>
#include "dictionary.h"
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include <ctype.h>
#define HASHTABLE_SIZE 10000
#define head hashtable[s]

typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

node *hashtable[HASHTABLE_SIZE];

//hash function (https://www.reddit.com/r/cs50/comments/1x6vc8/pset6_trie_vs_hashtable/)
int hash(char *word)
{
    unsigned int hash = 0;
    for (int i=0, n=strlen(word); i<n; i++)
    hash = (hash << 2) ^ word[i];
    return hash % HASHTABLE_SIZE;
}

int counter = 0;

// Returns true if word is in dictionary else false
bool check(const char *word)
{

    int n = strlen(word);

    char low[n + 1];

    for ( int i = 0; i < n; i++)
    {
        low[i] = tolower(word[i]);
    }

    //adding null character to the end
    low[n] = '\0';
    //allocating space for cursor
    node *cursor = malloc(sizeof(node));
    //finding which array that word is in
    int x = hash(low);
    //pointing cursor to where hashtable[x] is pointing
    cursor = hashtable[x];
    //unless cursor reaches end
    while (cursor != NULL)
    {
        //if word and the word in the cursor are same
        if (strcasecmp(low , cursor -> word) == 0)
        {
            return true;
        }
        //move cursor to the next word in the hashtable
        cursor = cursor -> next;
    }
    return false;
}

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

    if (file == NULL)
    {
        printf("File does not exist.\n");
        return false;
    }


    char word[LENGTH + 1];

    //scans word by word
    while (fscanf(file, "%s", word) != EOF)
    {
        //memory for new node
        node *new_node = calloc(1, sizeof(node));
        //check if malloc succeeds
        if (new_node == NULL)
        {
            unload();
            return false;
        }
        //choosing where the word will be on the hashtable array
        int s = hash(word);

        //copy word to new_node
        strcpy(new_node->word, word);
        //check if a single array in the hashtable is not pointing at the word(check if empty)
        if (head == NULL)
        {
            head = new_node;
        }
        //check if a single array in the hashtable is pointing at a word already
        else
        {
            //adding new word to the linked list
            //new_node pointing at what head is pointing
            new_node->next = head;
            //head points to the new node
            head = new_node;
        }
        counter++;
    }

    fclose(file);
    return true;
}

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

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    for (int i = 0; i < HASHTABLE_SIZE; i++)
    {
        node *cursor = hashtable[i];

        while (cursor != NULL)
        {
            node *temp = cursor;
            cursor = cursor -> next;
            free(temp);
        }
    }
    return true;
}
2

Valgrind is reporting leaked memory when I run it, and it comes from this line in check:

//allocating space for cursor
node *cursor = malloc(sizeof(node));

cursor is a pointer. It doesn't need any space allocated for it. This memory is never freed, which valgrind is reporting. Best to just declare it without mallocing.

1
  • Oh yea zamayla said that lol. Thanks! – sksch1663 Sep 18 '18 at 1:04

You must log in to answer this question.

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