0

I figured out that the problem is the checker. The output of this code is a segmentation fault. I think I followed everything on the walkthrough. Here is the result that I got: the result of the code. Thank you!

// Implements a dictionary's functionality

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

// Hash table
node *table[N];

// Number of words in the dictionary
int dic = 0;

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    //printf("Start check\n");
    bool checker;
    // Hash word to obtain a hash value
    unsigned int index = hash(word);
    // Access linked list at that index in the hash table
    // Transvers linked list, looking for the word (strcasecmp)
    node *head = table[index];
    node *cursor = head;
    while (cursor->next != NULL)
    {
        //printf("Good here\n");
        if (strcasecmp(cursor->word, word) == 0)
        {
            checker = true;
        }
        cursor = cursor->next;
    }
    if (cursor == NULL || strcasecmp(cursor->word, word) != 0)
    {
        checker = false;
    }
    //printf("End check\n");
    return checker;
}

// Hashes word to a number
// djb2 hash function, Retrived from: http://www.cse.yorku.ca/~oz/hash.html
unsigned int hash(const char *word)
{
    // //printf("Start hash\n");
    bool accept = false;
    int result = 0;
    int k = strlen(word) + 1;
    for (int i = 0; i < k; i++)
    {
        if (isalpha(word[i]) || word[i] == '\'')
        {
            accept = true;
        }
    }
    if (accept == true)
    {
        unsigned long hash = 5381;
        int c;
        while ((c = *word++))
        {
            hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
        }
        result = hash % N;

    }
    else
    {
        result = 0;
    }
    // //printf("End hash\n");
    return result;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // Open dictionary file
    //printf("Start load\n");
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        return false;
    }
    // Read strings from file one at a time
    char *buffer = malloc(LENGTH + 1);
    while (fscanf(file, "%s", buffer) != EOF)
    {
        // Create a new node for each word
        node *new_word = malloc(sizeof(node));
        if (new_word == NULL)
        {
          return false;
        }
        strcpy(new_word->word, buffer);
        // Hash word to obtain a hash value
        unsigned int index = hash(new_word->word);
        // Insert node into hash table at that location
        node *head = table[index];
        while (head != NULL)
        {
          new_word->next = table[index];
        }
        head = new_word;
        dic++;
    }
    fclose(file);
    //printf("End load\n");
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    //printf("Start size\n");
    //printf("End size\n");
    return dic;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    //printf("Start unload\n");
    for (int i = 0; i < N; i++)
    {
        node *head = table[i];
        node *cursor = head;
        node *tmp = cursor;
        while (cursor != NULL)
        {
            cursor = cursor->next;
            free(tmp);
            tmp = cursor;
        }
        if (table[i] == NULL)
        {
            return true;
        }
        //printf("End unload\n");
    }
    return false;
}
1
  • can you combine them as one block so someone can compile/run/debug for you? – stensal Jun 17 '20 at 11:50
0
    node *head = table[index];  
    node *cursor = head;  // head is NULL. 
    while (cursor->next != NULL)  // cursor is NULL, it causes segfault here. 
    {

The reported memory access error for this segfault.

 Memory access error: dereferencing a non-pointer; abort execution.
  # Reading 4 bytes from an illegit address (0x30).
  #
  # Stack trace (most recent call first) of the read.
  # [0]  file:/dictionary.c::40, 5
  # [1]  file:/speller.c::113, 32
  # [2]  [libc-start-main]

Here is the link to reproduce/debug the following error.

1
  • Thanks! I guess I'll try to lower case the letters when I hash it – Aladin Oliver Yu Jun 20 '20 at 12:28

You must log in to answer this question.

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