Working through the load portion of pset5. Everything compiles, but when I run valgrind it looks as if nothing is actually loading in, with the error of "null pointer passed as argument 1, which is declared to never be null."
In valgrind it is pointing to the following code within my hash function:
int hash(char *w) // Hash function
{
hash_value = 1;
int len = strlen(w);
....}
However I think the error is actually taking place when inserting new values into the hashtable:
FILE* dict = fopen(dictionary, "r");
char word[LENGTH+1];
if (dict == NULL)
{
fprintf(stderr, "Invalid Dictionary");
return 1;
}
while (fscanf(dict, "%s", word) != EOF)
{
node* new_node = malloc(sizeof(node));
// Check if malloc succeeeded
if (new_node == NULL)
{
//unload(dict);
return 1;
}
word_count++;
//new_node->word = word; //Insert Word into the nodes "word" section
//Hash value.
hash_value = hash(word);
strcpy(new_node->word, word);
if (hashtable[hash_value] == NULL)
{
hashtable[hash_value] = new_node;
new_node->next = NULL;
}
else
{
new_node->next = hashtable[hash_value];
hashtable[hash_value] = new_node;
}
}
Do I have to do something different since I'm inserting into a hashtable? I initialize hashtable earlier on as follows:
node* hashtable[15625]; //Number of buckets for hashtable
Any hints or pointers appreciated!