0

My first post here. I am fighting the 'speller' exercise for 3-4 weeks already, on and off. I think I've got the core of the code, but I keep running into a segmentation fault. Could someone please give me a tip?

THANK YOU

// Implements a dictionary's functionality

#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <cs50.h>
#include <stdio.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;

// Size value
int sizez = 0;

// Number of buckets in hash table
const unsigned int N = LENGTH + 1;

// Hash table
node *table[N];

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    int X = (hash (word));
    node *cursor1 = table[X];
    int result = 0;

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

    cursor1 = NULL;
    return false;
}

// Hashes word to a number based on the stringlength
unsigned int hash(const char *word)
{
    int size_t = strlen (word);

//JUST IN CASE the word is somehow longer than N
    if (size_t >= N)
    {
        size_t = size_t % N;
    }

// do -1 in order to use the [0] array of the hash table, if the number is only 1 letter, because one letter + "\0" equals already 2, and I want 0.
    int hash = size_t -2;
    return hash;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    FILE *file = fopen(dictionary, "r");
    if (!file)
    {
        return false;
    }

    char word[LENGTH + 1];

    while (fscanf(file, "%s", word) != EOF)
    {
        node *n = malloc(sizeof(node));

        if (n == NULL)
        {
            unload();
            return false;
        }
        strcpy (n->word, word);
//get hash value
        int X = hash(n->word);

//insert the node into the table
        if (table[X] == NULL)
        {
            table[X] = n;
            n->next = NULL;
        }
        else
        {
            n->next = table[X]->next;
            table[X]->next = n;
        }

//Move file pointer forward
        int position = strlen(word)+1;
        fseek(file, position, SEEK_CUR);
        position = 0;
        sizez = sizez + 1;
    }
    fclose(file);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    return sizez;
}

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

    do
    {
        result = strcasecmp(word, cursor1->word); // cursor1 is NULL here. 

The error messsage:

  Memory access error: dereferencing a null pointer; abort execution.
  # Reading 1 bytes from address 0x0.
  #
  # Stack trace (most recent call first) of the read.
  # [0]  file:/musl-1.1.10/src/string/strcasecmp.c::8, 2
  # [1]  file:/dictionary.c::40, 18
  # [2]  file:/speller.c::113, 32
  # [3]  [libc-start-main]

The link to reproduce/debug segfault for your code.

5
  • Thanks man! That one I've fixed (added an if clause checking if table[X] is NULL). Still have two issues: valgrind is showing a leak in line 87 (of the new code), which is: node *n = malloc(sizeof(node)) . Could you please give me a hint with that? Also, the speller misses on when checking some of the words, but I guess I will find the errors there myself, at least the segmentation fault is no longer there! :) – Elvinas Didika Jun 15 '20 at 18:53
  • Can you show me your latest code? – stensal Jun 15 '20 at 23:01
  • your hash function produces a negative value and cause memory error. the link to reproduce/debug this error. Click "Start", and run speller small lalaland.txt in the terminal. – stensal Jun 16 '20 at 7:53
  • THANK YOU! All sorted now :) – Elvinas Didika Jun 16 '20 at 15:15

You must log in to answer this question.

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