0

I am currently trying to master speller but getting an error in one specific line when I run valgrind. It mentions this line:

n ->next = table[index];

I have no idea why. It says invalid read of size 8. (Process terminating with default action of signal 11 (SIGSEGV) ==6596== Access not within mapped region at address 0x5F98F0)

Also I am wondering if it is necessary to initialize my hash table, as I have done so at the very top of my 'load' function.

Thank you for any answers.

// Implements a dictionary's functionality

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

//global variable
int *count = NULL;


// Hash table
node *table[N];


// Hashes word to a number
unsigned int hash(const char *word)
{
    long long sum = 0;
    int len = strlen(word);
    for (int i = 0; i < len; i++)
    {
        sum = sum + pow((word[i] - 96), i);
    }

    int checksum = sum % 10000;
    return checksum;
}




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

    for (int i = 0; i < N; i++)
    {
        table[i] = malloc (sizeof(node));
        if (table[i] == NULL)
        {
            printf("ERROR: Could not allocate enough memory. /n");
            return false;
        }

        table[i]->next = NULL;
    }





    FILE * dict = fopen(dictionary, "r");
    if (dict == NULL)
    {
        return false;
    }

    char s[LENGTH+1];

    while (fscanf(dict, "%s", s) != EOF)
    {
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            unload();
            return false;
        }

        strcpy(n->word, s);
        int index = hash(n ->word);
        n ->next = table[index];
        table[index] = n;


        free(n);
    }


   fclose(dict);

    return true;
}

1 Answer 1

0

It's not really a "valgrind error", it's a straight up segmentation fault. There are a couple of issues going on here. I think I'll just give you a couple of big hints.

First, what does your hash function return for a word with an apostrophe, like "cat's" ?

Second, what happens to the node that you just created in load() when you free(n)?

There may be more issues, but this will get you going. ;-)

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

2
  • Thank you. I have replaced my hash function with: unsigned int hash(const char *word) { int hash = 0; for (int i = 0, n = strlen(word); i < n; i++) hash = (hash << 2) ^ word[i]; return hash % N; } I don't fully get it, but I'll come back to it later. Bc first I want to get my code to run. I have erased the' free(n)' line. I still get a couple of errors, but the free(n) definetly doesn't belong there, so thank you for the hint! I still have a question though: If I run the code without initializing the hash table, it gives way fewer errors. But isn't it goo May 8, 2020 at 10:18
  • *considered to be 'good programming practice' to initialize pointers? otherwise, they could be pointing to garbage values? May 8, 2020 at 10:26

You must log in to answer this question.

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