0

I've been stuck on speller for a few days now, and recently I've gotten the program to completely pass check50. However, when I try to run it manually on my own, I find that the program works fine with small text files and/or dictionaries but runs into segmentation faults when larger versions of either one is used.

For reference, these are the command line arguments I've been using:

./speller dictionaries/small texts/cat.txt (runs fine)

./speller dictionaries/large (seg. fault)

./speller dictionaries/small (misspelled words printed halfway followed by seg. fault)

I'm stuck on this and can't seem to solve it even after running over my code many times. Any help would be greatly appreciated. Thank you!

my code for reference:

// TODO: Choose number of buckets in hash table
const unsigned int N = 1125;

// Hash table
node *table[N];

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    // TODO
    int sum = 0;
    for (int i = 0; i < strlen(word); i++)
    {
        sum += toupper(word[i]) - 65;
    }
    node *ptr = table[sum];
    while (ptr != NULL)
    {
        if (strcasecmp(ptr -> word, word) == 0)
        {
            return true;
        }
        ptr = ptr -> next;
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    // TODO: Improve this hash function
    int sum = 0;
    for (int i = 0; i < strlen(word); i++)
    {
        sum += toupper(word[i]) - 65;
    }
    return sum;
}

//used for word count in size
int sz = 0;
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    FILE *file =  fopen(dictionary, "r");
    if (file != NULL)
    {
        char buffer[LENGTH + 1];
        while (fscanf(file, "%s", buffer) != EOF)
        {
            sz++;
            int index = hash(buffer);
            if (table[index] == NULL)
            {
                table[index] = malloc(sizeof(node));
                strcpy(table[index] -> word, buffer);
                table[index] -> next = NULL;
            }
            else
            {
                node *tmp = table[index];
                table[index] = malloc(sizeof(node));
                strcpy(table[index] -> word, buffer);
                table[index] -> next = tmp;
            }
        }
        fclose(file);
        return true;
    }
    return false;
}

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

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

1 Answer 1

1

Take a close look at the hash function. What happens if a word contains a non-alpha, such as an apostrophe? What values are returned?

Try inserting this into your hash function to diagnose:

printf("SUM: %i, word:%s\n",sum, word);

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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