0

I've been completing the fourth problem set on the CS50 course. After checking multiple times for errors in my code I finally managed to make it compile. The program works as it finds all of the misspelled words however once that is done, the program just keeps on running forever and never says how many words were misspelled nor how long it took to run.

Valgrind said it had too many errors and that it would not report any more however it says 0 out of 0 bytes are lost.

Here is my code load and check code. Thank you for the help!

Load:

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // Initialize hash table
    for (int i = 0; i < N; i++)
    {
        hashtable[i] = NULL;
    }

    // Open dictionary
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        unload();
        return false;
    }

    // Buffer for a word
    char word[LENGTH + 1];

    // Insert words into hash table
    while (fscanf(file, "%s", word) != EOF)
    {
        //add word to counter
        word_counter++;

        // create node for new word
        node *new_node = malloc(sizeof(node));
        if (new_node == NULL)
        {
            unload();
            return false;
        }

        //copy the word into the new node
        strcpy(new_node->word, word);

        //check for already existing linked list in hashtable
        if (hashtable[hash(word)] == NULL)
        {
            //hashtable pointer point to the new node
            hashtable[hash(word)] = new_node;
        }

        else
        {
            //change pointer of the new node to the already existing beginning of link list node
            new_node->next = hashtable[hash(word)];

            //then change the hashtable's first pointer to the new_node
            hashtable[hash(word)] = new_node;
        }
    }

    // Close dictionary
    fclose(file);

    // Indicate success
    return true;
}

Check:

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    // set check pointer
    node *cursor = hashtable[hash(word)];

    // EOF loop
    while (cursor != NULL)
    {
        if (strcasecmp(word, cursor->word) != 0)
        {
             cursor = cursor->next;
        }

        else
        {
            return true;
        }
    }
    return false;
}
1

I assume you have an infinite loop in unload, check that one. Maybe edit it into your question if you cannot find the mistake.

There's definitely a problem with this snippet:

    if (hashtable[hash(word)] == NULL)
    {
        //hashtable pointer point to the new node
        hashtable[hash(word)] = new_node;
    }

new_node->next is not initialised, so its value is undefined (whatever was at that location in memory, likely NULL, but who knows). Do not check whether hashtable[hash(word)] == NULL, just always new_node->next = hashtable[hash(word)]; (or assign the return value of hash(word) to some variable, so you don't have to call the function multiple times).

2
  • Thank you very much, I I had never thought that my unload would have been the problem. I finally found the stuck loop and managed to fix it. :) – fazzo May 1 '19 at 17:00
  • If it prints the misspelled words, it's likely something called after check. Glad my first guess pointed you in the right direction. – Blauelf May 2 '19 at 6:43

You must log in to answer this question.

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