0

I'm writing a program that spell checks words, but my hash function is not returning the same number for the same words.

My question is how is my hash function not returning the same hash for the same input.

Here is a Minimal, Reproducible Example of my issue:

// Implements a dictionary's functionality

#define HASHTABLE_SIZE 65536

// 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 = HASHTABLE_SIZE;

// Hash table
node *table[N];
unsigned int totalWords = 0;

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

    for (int i=0, n=strlen(word); i<n; i++)
        hash_value = (hash_value << 2) ^ word[i];

    return hash_value % HASHTABLE_SIZE;
}

Nicolas F
  • 505
  • 6
  • 17
  • 1
    Provide sample input, expected output, actual output and describe what your program is trying to do. – rinz1er Jun 27 '20 at 16:18
  • the posted code does not compile! To start, the function: `hash()` is called before the compiler knows its' signature. Suggest, before the first function, to insert a prototype for `hash()` Next: `node *table[N];` results in: *untitled1.c:26:7: error: variably modified ‘table’ at file scope* Then your homegrown file: `directory.h` needs to have its' contents posted – user3629249 Jun 28 '20 at 01:11
  • When compiling, always enable the warnings, then fix those warnings. ( for `gcc`, at a minimum use: `-Wall -Wextra -Wconversion -pedantic -std=gnu11` ) Note: other compilers use different options to produce the same results – user3629249 Jun 28 '20 at 01:15

2 Answers2

2

hash_value in the hash function is uninitialized and it wreaks memory havoc, which causes unpredictable results. From the cited post:

unsigned int hash = 0;

DinoCoderSaurus
  • 6,110
  • 2
  • 10
  • 15
1

Your fscanf writes to the outside of this memory block pointed by word.

    char *word = malloc(LENGTH);  // this is too small to hold a word + '\0'
    ...
    while (fscanf(dicfile, "%s", word) != EOF)
    {

Increasing the size to LENGTH+1.

stensal
  • 401
  • 4
  • 8