我在以下问题上打了几个小时: 我粘贴了2个功能,但还有更多功能。我在我的程序上运行valgrind,我得到32个类似的错误:
==4214== 6 errors in context 8 of 10:
==4214== Conditional jump or move depends on uninitialised value(s)
==4214== at 0x40088F: getNextFreeCell (in /a/fr-01/vol/home/stud/ashers03/c/ex4/test)
==4214== by 0x400C7A: InsertObject (in /a/fr-01/vol/home/stud/ashers03/c/ex4/test)
==4214== by 0x401137: main (in /a/fr-01/vol/home/stud/ashers03/c/ex4/test)
我在其他功能上遇到更多错误,但是同样的错误。我无法理解为什么它没有被初始化。谢谢大家的帮助。
这是主要功能:
int main(int argc, char* argv[]) {
size_t tableSize = (size_t)atoi(*(argv+1));
TableP table = CreateTable(tableSize,IntFcn, IntPrint,IntCompare);
int i;
for (i=FIRST; i<=LAST; i++) {
int *key = (int*)malloc(sizeof(int));
*key = i;
ObjectP obj = CreateObject(key);
InsertObject(table,obj);
}
PrintTable(table);
FreeTable(table);
return 0;
}
这些defs位于头文件中:
typedef struct Object* ObjectP;
typedef struct Table* TableP;
typedef const struct Table* ConstTableP;
typedef enum {FALSE, TRUE} Boolean;
此代码位于另一个文件中:
typedef struct Table {
ObjectP* _table;
int _firstTableSize;
int _currentTableSize;
int _increaseFactor;
HashFcn _hfun;
PrintFcn _pfun;
ComparisonFcn _fcomp;
} Table;
typedef struct Object {
ObjectP _next;
void* _key;
int _numInChain;
} Object;
此函数将一个键插入哈希表。如果3个键已经链接在单元格中,那么表的大小加倍,我在doubleTable()中做了其他一些事情......
Boolean InsertObject(TableP table, ObjectP object) {
int index=table->_increaseFactor*table->_hfun(object->_key,table->_firstTableSize);
if (table->_table[index] != NULL) {
if (table->_table[index]->_numInChain == MAX_CHAIN) { //search for next cell
int nextFreeCell = getNextFreeCell(table,index+1);
if (nextFreeCell == FAILED) { //double table size
if(doubleTable(table)) {
InsertObject(table,object);
return TRUE;
}
else {
ReportError(MEM_OUT);
return FALSE;
}
}
else {
table->_table[nextFreeCell] = chainObject(table->_table[nextFreeCell],object);
return TRUE;
}
}
else { //place object in chain:
table->_table[index] = chainObject(table->_table[index],object);
return TRUE;
}
}
else { //empty cell, place object
table->_table[index] = chainObject(table->_table[index],object);
return TRUE;
}
}
static int getNextFreeCell(TableP table, int index) {
int tableSize = table->_currentTableSize;
while ( (index < tableSize) && (index % table->_increaseFactor != 0) ) {
if (table->_table[index] == NULL || table->_table[index]->_numInChain < MAX_CHAIN) {
return index;
}
index++;
}
return FAILED;
}
编辑:
我按照你的说法跑了valgrind,我得到了:==4563== Conditional jump or move depends on uninitialised value(s)
==4563== at 0x40088F: getNextFreeCell (GenericHashTable.c:75)
==4563== by 0x400C7A: InsertObject (GenericHashTable.c:222)
==4563== by 0x401137: main (HashIntMain.c:34)
==4563== Uninitialised value was created by a heap allocation
==4563== at 0x4C241A7: malloc (vg_replace_malloc.c:195)
==4563== by 0x4007AF: allocateArray (GenericHashTable.c:41)
==4563== by 0x400924: doubleTable (GenericHashTable.c:90)
==4563== by 0x400C8F: InsertObject (GenericHashTable.c:225)
==4563== by 0x401137: main (HashIntMain.c:34)
我有这个方法:
static ObjectP* allocateArray(int tableSize) {
objectP* arr = (ObjectP*)malloc(tableSize * sizeof(ObjectP));
return arr;
}
这会创建一个我从未初始化的指针数组。这可能是问题吗?以及我应该如何初始化指针数组?为NULL?
答案 0 :(得分:31)
您需要使用--track-origins=yes
选项运行valgrind以查找未定义值的来源。
答案 1 :(得分:4)
问题是我在创建时没有初始化指针数组。
答案 2 :(得分:2)
在我看来,您没有使用调试标志编译程序(gcc为-g
)。然后,如果运行带有所有选项的valgrind,它应该准确地告诉您哪些变量会导致问题。
答案 3 :(得分:2)
您在哪里初始化table->_table
?我猜想,检查你是否正确地初始化它CreateTable()
。如果没有任何明显的消息,请发布该功能的代码。
答案 4 :(得分:1)
简单 - 只需运行
{{1}}
......找出答案。