这个函数是哈希表的一部分。我正在传递一个要搜索的哈希列表和一个键。
int HashSearch (Hash_list h, char* key)
{
struct ent *x = (struct ent *)malloc(10*sizeof(struct ent));
for (x = h->table[hash(key, h->size)]; x!=0; x=x->next) {
if (strcmp(x->pos, key)==0) {
return x->num;
}
}
return -1;
}
不幸的是,当我尝试运行它时,valgrind 一直给我错误
==1741== Conditional jump or move depends on uninitialised value(s)
==1741== at 0x4018DE: HashSearch (Hash.c:81)
==1741== by 0x400FA7: function (Nine13.c:181)
==1741== by 0x4009D8: main (Nine13.c:54)
==1741==
==1743== Conditional jump or move depends on uninitialised value(s)
==1743== at 0x4018DE: HashSearch (Hash.c:81)
==1743== by 0x400FA7: function (Nine13.c:181)
==1743== by 0x4009D8: main (Nine13.c:54)
==1743==
(第 81 行是以“for”开头的代码行)。
我觉得我已经初始化了 struct ent
和传入的哈希列表。有什么问题吗?
最佳答案
基于 this other question , 看起来问题确实是不是 h->table
中的所有条目都被初始化了。请注意,虽然在 table
上使用 calloc
或 memset
很有可能会解决问题,但从技术上讲,它不可移植。虽然 0
保证评估等于空指针,但位模式不能保证全部为 0
。此外,正如在其他线程和我上面的评论中指出的那样,对于复杂的数据类型,您通常需要一些其他的“空”值。
关于c - 初始化 Valgrind 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16096219/