0

I have been wrapping my head around this pset for several days and I have no clue on what is causing problems for my code (all words are 'mispelled'). Debug50 hasn't really been helpful :( I suppose that the problem might lie in the check or load function.

Besides, valgrind doesn't result to any memory leaking error. I would be eternally grateful if someone can help me out with this.

Below is my code:

#include <stdio.h>
#include <stdbool.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include "dictionary.h"

//for 'size' function
int sizeCount = 0;

//Sets up pointer for linked list
typedef struct node
{
    char word[LENGTH + 1];
    struct node* next;
}
node;
node* head[4096];

//hash function
int hash_func(const char *word)
{
    unsigned int hash = 0;
    for (int i = 0, n = strlen(word); i < n; i++)
    {
        hash = (hash << 2) ^ word[i];
    }
    return hash % 4096;
}

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    int n = strlen(word);
    char lowercased_word[n+1];
    strcpy(lowercased_word, word);

    //copy string, make it lowercase
    for (int i = 0; i <= n; i++)
    {
        lowercased_word[i] = tolower(lowercased_word[i]);
    }

    //checking if the word matches the result from dictionary
    int result = 0;
    int index = hash_func(lowercased_word);
    node *cursor = head[index];

        while (cursor != NULL)
        {
            result = strcmp(cursor->word, lowercased_word);
            if (result == 0)
            {
                return true;
            }
            cursor = cursor->next;
        }

    return false;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    //opening the dictionary
    FILE* inptr = fopen(dictionary, "r");

    //check file for null
    if (inptr == NULL)
    {
        printf("There is a problem with the provided dictionary file.");
        return false;
    }

   //setting all the head pointers to NULL
    for (int i = 0; i < 4096; i++) {
        head[i] = NULL;
    }

    //scan each word
    char word[LENGTH+1];
    while (fscanf(inptr, "%s", word) != EOF)
    {
        //hash the word
        int index = hash_func(word);

        //creating a node for each word
        node *new_node = malloc(sizeof(node));

        //malloc should succeed
        if (new_node == NULL)
        {
            unload();
            return false;
        }

        //add dictionary word to the linked list
        strcpy(new_node->word, word);
        new_node->next = NULL;

        if(head[index])
        {
            new_node->next = head[index];
            head[index] = new_node;
        }

        // in case of head is pointing to null
        else {
            head[index] = new_node;
            new_node->next = NULL;
        }
        sizeCount++;
        unload();
    }
    //free memory (word)
    fclose(inptr);
    return true;
}

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

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    for (int i =0; i < 4096; i++)
    {
        node *cursor = head[i];
        while (cursor != NULL)
        {
            node *temp = cursor;
            cursor = cursor->next;
            free(temp);
        }
        head[i] = NULL;
    }
    return true;
}
2

What's that rogue unload(); doing at the end of your main loop in load?

2
  • wow I have never questioned myself about the reason I wrote that line. Initially, I might have written it for the sake of freeing the memory of new_node. Now I have deleted it and it's working like a charm. Thanks a lot for your help! – knighttoken Dec 12 '18 at 14:03
  • Glad I could help, even if, or maybe especially because, it was very little I had to do :) – Blauelf Dec 12 '18 at 18:00

You must log in to answer this question.

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