When I compile and run my code it sort of looks like the code works, but when I run Valgrind with help50, it says "==1295== Conditional jump or move depends on uninitialized value(s)" I don't get how to fix this problem. Help50 tells me to focus on line 54 of my code, but I don't understand what is wrong, it was working before, now it is a mistake. What I don't understand is what the mistake is/
#include <ctype.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.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;
int i = 0;
// Hash table
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
char lword[LENGTH+1];
for(i = 0; i < strlen(word); i++)
{
lword[i] = word[i];
lword[i] = tolower(lword[i]);
}
node *current;
int hashnum = hash(lword);
if(table[hashnum] == NULL)
return false;
current = table[hashnum];
while(current->next != NULL)
{
if(strcmp(current->word, word) == 0)
return true;
else
current = current->next;
}
return false;
}
// Hashes word to a number
// Hash function from cs50.stackexchange
unsigned int hash(const char *word)
{
int n;
unsigned int hash_value = 0;
for (i = 0, n = strlen(word); i < n; i++)
{
hash_value = (hash_value << 2) ^ word[i];
}
return hash_value % N; //N is size of hashtable
}
// Loads dictionary into memory, returning true if successful else false
// adopted from github user
int word_count = 0;
bool load(const char *dictionary)
{
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
return false;
}
char word[LENGTH + 1];
while (fscanf(file, "%s", word) != EOF)
{
node *new_node = malloc(sizeof(node));
if (new_node == NULL)
{
free(new_node);
return false;
}
strcpy(new_node->word, word);
int h = hash(new_node->word);
node *head = table[h];
if (head == NULL)
{
table[h] = new_node;
word_count++;
}
else
{
new_node->next = table[h];
table[h] = new_node;
word_count++;
}
}
fclose(file);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return word_count;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
for(i=0;i<10000;i++)
{
for(i = 0; i < 10000; i++)
{
while(table[i] != NULL)
{
char *retval = NULL;
if (table[i]->next == NULL)
{
retval = table[i]->word;
free(table[i]);
return retval;
}
else
{
node * current = table[i];
while (current->next->next != NULL)
{
current = current->next;
}
retval = current->next->word;
free(current->next);
current->next = NULL;
}
}
}
}
return true;
}