0

I am curently working pset5 speller in the load part and, when run with ./speller texts/lalaland.txt, returns the following

dictionary.c:68:32: runtime error: member access within null pointer of type 
'node' (aka 'struct node')
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==2130==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 
0x000000000030 (pc 0x0000004246dc bp 0x7ffe8dc0ff80 sp 0x7ffe8dc0fdb0 T2130)
==2130==The signal is caused by a WRITE memory access.
==2130==Hint: address points to the zero page.
    #0 0x4246db  (/home/ubuntu/environment/pset4/speller/speller+0x4246db)
    #1 0x4226b2  (/home/ubuntu/environment/pset4/speller/speller+0x4226b2)
    #2 0x7fb32d6f3b96  (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #3 0x402d19  (/home/ubuntu/environment/pset4/speller/speller+0x402d19)

UndefinedBehaviorSanitizer can not provide additional info.
==2130==ABORTING

Here is my code up until now:

// Represents number of buckets in a hash table
#define N 26

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// Represents a hash table
node *hashtable[N];

// Hashes word to a number between 0 and 25, inclusive, based on its first 
letter
unsigned int hash(const char *word)
{
    return tolower(word[0]) - 'a';
}

//Creates value for size of dictionary
long long n = 0;

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)

{ // Initialize hash table for (int i = 0; i < N; i++) { hashtable[i] = NULL; }

// Open dictionary
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
    unload();
    return false;
}

// Buffer for a word
char word[LENGTH + 1];

// Insert words into hash table
while (fscanf(file, "%s", word) != EOF)
{
    //mallocs space for new word
    node *new_node = malloc(sizeof(node));
    if (new_node == NULL)
    {
        unload();
        return false;
    }
    strcpy(new_node->word, word);
    //gets the hash value of word
    int index1 = hash(word);
    //checks if hashtable[index] is pointing to a node already
    if (hashtable[index1] == NULL)
    {
        hashtable[index1]->next = new_node;
    }
    else
    {
        new_node->next = hashtable[index1]->next;
        hashtable[index1]->next = new_node;
    }
    n++;

}

// Close dictionary
fclose(file);

// Indicate success
return true;

}

Here is line 68 which seems to cause the problem

hashtable[index1]->next = new_node;

Anybody knows why this happens? All help is appreciated

1 Answer 1

2

Pretty straight forward. Look at the code:

if (hashtable[index1] == NULL)
{
    hashtable[index1]->next = new_node;
}

The assignment statement is only going to execute if hashtable[index1] is NULL. That means that whenever it executes, hashtable[index1] doesn't point at anything. It points to address 0. So, hashtable[index1]->next doesn't exist, causing the error.

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

You must log in to answer this question.

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