0

I need help with my unloads function. I keep getting a segmentation fault on "while(cursor->next != NULL)." When I hover over the "cursor->next" part it says no value. I don't understand what's wrong, can someone explain? Thank you so much.

    // Implements a dictionary's functionality

#include <stdbool.h>

#include "dictionary.h"

#include <strings.h>

#include <string.h>

#include <stdio.h>

#include <stdlib.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 = 40;

// Hash table
node *table[N] = {NULL};

//counter for load
int count = 0;

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    int num = 0;

    num = hash(word);

    node* traversal = malloc(sizeof(node));

    traversal = table[num];

    while(traversal->next != NULL)
    {
        if(strcasecmp(traversal->word, word) == 0)
        {
            return true;
        }
        traversal = traversal->next;
    }

    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    unsigned int hash = 5381;
    int c;

    while ((c = *word++))
    {
        /* hash * 33 + c */
        hash = ((hash << 5) + hash) + c;
    }

    return hash % N;

}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    unsigned int num = 0;

    char buffer[LENGTH + 1];

    //Open dictionary file
   FILE *ptr = fopen(dictionary, "r");

   if(ptr == NULL)
   {
       printf("Can't open dictionary");
       return false;
   }
    //Read string from file one at a time
    while(fscanf(ptr,"%s",buffer) != EOF)
    {
        //Create a new node for each word
        node* n =malloc(sizeof(node));
        if(n != NULL)
        {
            strcpy(n->word,buffer);
            n->next = NULL;
        }
        //Hash word to obtain a hash value for each word
        num = hash(buffer);


        //Check to see if something already at location
        if(table[num] == NULL)
        {
             n->next = NULL;
        }

        else
        {
            n->next = table[num];
        }
        //Insert node into hash table at that location
        table[num] = n;

        //keeps track of how many words in dictionary
        count++;

        free(n);
    }

    return true;
}

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

    else return 0;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    node* cursor = NULL;
    node* temp = NULL;

    for(int i = 0; i<N; i++)
    {
        cursor = table[i];
        temp = cursor;
        while(cursor->next != NULL)
        {
            temp = cursor->next;
            free(cursor);
            cursor = temp;
        }
    }
    return true;
}
1

If table[index] is not populated, there is not such a thing as table[index]->next (or table[index]->word). The same bug exists in check. Run with the small dictionary to see that in action.

| improve this answer | |

You must log in to answer this question.

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