I am getting the following error from valgrind. Conditional jump or move depends on uninitalized values. I have looked around at similar questions but I am not able to find out whats wrong. I have initialized all the variables, but still..
unsigned long hash_word(const char *str)
{
unsigned long hash = 5381;
int c = 0;
while((c = *str)) // The error occurs here
{
hash = ((hash<<5) + hash) + c;
str++;
}
return hash%1999099;
}
The value of str is passed from the main function. I am using leak-check=full and track-origins=yes. Thanks in advance for the help.
First I am initializing a node.
typedef struct node{
char word[46];
struct node *next;
} node;
The calling code is
while(!(feof(fp)))
{
node *n = malloc(sizeof(node));
if (n == NULL)
{
return false;
}
else
{
fscanf(fp,"%s",n->word);
index = hash_word(n->word);
.
.
. // further code
}
This looks like a misunderstanding in what
feof()does. It doesn’t return a true value until after a read has failed due to EOF. Therefore, in your last iteration, thefscanf()call fails and thus does not initializen->word. You should check the return value offscanf(). If it hits EOF it returns the C valueEOF. You could also check for the value1indicating a single field was successfully converted.