我从valgrind收到以下错误。条件跳转或移动取决于未初始化的值。我已经查看了类似的问题,但我无法找出错误。我已经初始化了所有变量,但仍然......
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;
}
str的值从main函数传递。我使用leak-check = full和track-originins = yes。在此先感谢您的帮助。
首先我正在初始化一个节点。
typedef struct node{
char word[46];
struct node *next;
} node;
主叫代码是
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
}
答案 0 :(得分:3)
这似乎是对feof()
所做的误解。在读取因EOF失败后之前,它不会返回真值。因此,在上一次迭代中,fscanf()
调用失败,因此不会初始化n->word
。您应该检查fscanf()
的返回值。如果它达到EOF,则返回C值EOF
。您还可以检查值1
,表示单个字段已成功转换。