0

I'm new to CS and could really use some help on the speller pset. I've got a basic outline that doesn't seem to seg fault, but I'm still having issues. It fails check50 in that:

  • it doesn't handle most basic words properly
  • spell-checking is not case insensitive
  • and has memory errors If I run a tester file through it, the counter only shows that there are 2 words in the dictionary, and so it spits out nearly every word in the document as misspelled (leading me to think there is an error in load, but I can't figure out where).

There's also a valgrind error. It displays the following:

HEAP SUMMARY: in use at exit: 7,543,356 bytes in 154,643 blocks. total heap use: 153,054 allocs, 6,543 frees, 9,324,387 bytes allocated.

Any assistance would be much appreciated; I've been stuck on this for 3 days (and the more you can explain your reasoning, the better)!

#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.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];

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    int numloc = hash(word);
    node *cursor = table[numloc];
    while (cursor != NULL)
    {
        if (strcasecmp(cursor->word, word) == 0)
        {
            return true;
        }
        cursor = cursor->next;
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    unsigned long hash = 5381;
    int c;
    while ((c = *word++))
    {
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
    }
    return hash % N;
}

// Loads dictionary into memory, returning true if successful else false
int counter = 0;
bool load(const char *dictionary)
{
    FILE *dictionary_ = fopen(dictionary, "r");
    if (dictionary_ == NULL)
    {
        return false;
    }
    char tempword[LENGTH + 1];
    for (int i = 0; i < N; i++)
    {
        table[i] = NULL;
    }
    while (fscanf(dictionary_, "%s", tempword) != EOF)
    {
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            return false;
        }
        strcpy(n->word, tempword);
        int A = hash(tempword);
        if (table[A] == NULL)
        {
            table[A] = n;
            n->next = NULL;
        }
        else
        {
            n->next = table[A];
            table[A] = n;
        }
        counter++;
    }
    fclose(dictionary_);
    return true;
}


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

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

You must log in to answer this question.

Browse other questions tagged .