0

My speller.c seems to work, slow, but fine. Giving me the correct output when comparing with the staffs solution.

This is what check 50 (check50 2015.fall.pset5.speller dictionary.c dictionary.h Makefile) tells me... https://sandbox.cs50.net/checks/73adeb2cca014e1dbbd988d5cddbc60c

I have included my code, in case that it is me who have made some kind of stupid mistake...

Thank you!

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "dictionary.h"


/**
 * Returns true if word is in dictionary else false.
 */
 unsigned long sizeOfDict = 0;

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


 node* hashtable[65535];


 // Hashfunction djb2, http://www.cse.yorku.ca/~oz/hash.html
 unsigned long hash(const char *str)
    {
        unsigned long hash = 5381;
        int c;

        while ((c = *str++) != 0)
            hash = (((hash << 5) + hash) + c) % 65535; /* hash * 33 + c */

        return hash;
    }


bool check(const char* word)
{
    // TODO
    char wordCpy[strlen(word)+1];
    strcpy(wordCpy, word);
    for(int e = 0; e < strlen(wordCpy); e++) {
        wordCpy[e] = tolower(wordCpy[e]);
    }
    unsigned long bucket = hash(wordCpy);

    node* cursor = hashtable[bucket];
    while(cursor != NULL) {
        if (!strcmp(cursor->word, wordCpy)) {
            return true;
        }
        else {
            cursor=cursor->next;
        }
    }
    return false;
}

/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{
    // TODO

    FILE* dict = fopen(dictionary, "r");

    if(dict == NULL) {
        printf("File doesn't exist\n");
        return false;
    }

    while(!feof(dict)) {
        node* new_node = malloc(sizeof(node));
        fscanf(dict, "%s", new_node->word);
        unsigned long basket = hash(new_node->word);

        new_node -> next = hashtable[basket];
        hashtable[basket] = new_node;
        sizeOfDict++;
    }
    fclose(dict);
    return true;
}

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

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

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

    }
    free(hashtable);


    return true;
}
1

Had to add #include <stdlib.h> to get it to compile. Once that was done, it was giving a seg fault. The seg fault is in the unload function. You have a statement, free(hashtable); in unload, but the array hashtable was never malloc'd. It is created on the stack, so it cannot be free'd. This is causing a seg fault.

I'm curious why you didn't see the compile problem and why your code actually runs without generating a seg fault. Are you running on the cloud9 IDE or a different environment?

There's another bug in the code, but it's an easy fix that I'll let you find. ;-)

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

1
  • Thanks, it is all working now :) I'm using the cloud9 IDE, but it have behaved weird the whole day... – maxrimmer Mar 26 '16 at 17:08

You must log in to answer this question.

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