0

completely stuck in pset5 for so long... I rewrite my code several times and try to find any info from anywhere. But now seems still not working. compile speller with no error. when I try to run ./speller texts/austinpowers.txt Press enter, wait for a while, the terminal just return KILLED... when using valgrind to check memory leaks, it stuck there for a while and return something like no usage are freed... Here is my code below. Can anyone see any issue there?

/**
 * dictionary.c
 *
 * Computer Science 50
 * Problem Set 5
 *
 * Implements a dictionary's functionality.
 */

#include <stdbool.h>
#include <string.h>
//#include <strings.h>
#include <ctype.h> 
#include <stdio.h>
#include <stdlib.h>

#include "dictionary.h"
#define SIZE 65536

// declare the struct for hashtable and initiate the hashtable elements with NULL

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


// declare the hash table
node* hashtable[SIZE]; 

// hash function cited from go-through session of staff
/**********************
 * int hash(const char* key)
{
    // hash on first letter of the string
    int hash = tolower(key[0]) - 'a';
    return hash % SIZE; 
}

*******************/
int hash(const char* key)
{
    unsigned int hash = 0;
    char temp[strlen(key)];
    strcpy(temp, key);
    for (int i=0, n=strlen(key); i<n; i++)
    {
        temp[i] = tolower(temp[i]);
        hash = (hash << 2) ^ temp[i];
    }

    return hash % SIZE;
}

int count = 0; 
/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char* word)
{
    // hash the word to find which bucket it belongs to 
    int hash(const char* key);
    int bucket = hash(word);

    char temp[strlen(word)];
    strcpy(temp, word);

    for (int i=0, n=strlen(word); i<n; i++)
    {
        temp[i] = tolower(temp[i]);
    }

    node* cursor = hashtable[bucket];

    while (cursor != NULL)
    {
        if (strcmp(cursor->str, temp) == 0)
            return true; 
        else 
            cursor = cursor->next; 
    }

    return false;
}

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

    // initiate the hashtale elements to point to NULL
    for (int i = 0; i < SIZE; i++)
    {
        hashtable[i] = NULL; 
    }

    // open the dictionary to be loaded; declare node pointer for the word read 
    FILE* dic = fopen(dictionary, "r");

    if (dic == NULL)
    {
        printf("can't open dictionary file\n");

        return false; 
    }

    char buffer[LENGTH + 1];

    // iterate over each word in the dictionary; hash the word to find the right bucket; add the word to the bucket
    while (fscanf(dic, "%s", buffer) != 0)
    {
        node* new_node = malloc(sizeof(node)); 

        if (new_node == NULL)
        {
            printf("not enough heap memory\n");

            return false; 
        }
        strcpy(new_node->str, buffer);

        int bucket = hash(new_node->str); 

        node* cursor = hashtable[bucket]; 

        if (cursor == NULL)
        {
            hashtable[bucket] = new_node;
            new_node->next = NULL; 
        }
        else
        {
            new_node->next = cursor; 
            hashtable[bucket] = new_node;
        }

        count++; 
    }

    return true;
}

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

/**
 * Unloads dictionary from memory.  Returns true if successful else false.
 */
bool unload(void)
{
    // free every linked list for each bucket
    for (int i = 0; i < SIZE; i++)
    {
        node* cursor = hashtable[i];

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

    return true;
}
0

The while loop is going into an infinite loop. The cause lies here:

    while (fscanf(dic, "%s", buffer) != 0)

It doesn't return a 0 when it hits EOF. Instead of checking for a single unexpected result ( != 0 ), it should check for any read that doesn't produce the expected result, == 1. You might want to review the description of fscanf and it's return values for a better understanding.

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

  • Ha! I should have seen it. Thanks for point it out. – michael zhang Nov 22 '16 at 0:37

You must log in to answer this question.

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