我在以下問題上打破了我的頭幾個小時: 我粘貼了2個函數,雖然還有更多。我在我的程序運行Valgrind的,我也得到32個錯誤與此類似:有條件的跳轉或移動取決於未初始化的值(s)
==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;
}
這產生一個指針數組,WHI我從未初始化過。這可能是問題嗎?以及我應該如何初始化一個指針數組?爲NULL?
您是否使用「-g」調試信息編譯了代碼?如果是這樣,它會告訴你它呻吟的確切線條或變量? – bramp 2010-09-01 20:10:34
「InsertObject」函數中「{}」的對齊方式具有誤導性。 if(doubleTable(table)){'沒有「對齊」對之後的'{''。我不知道這是否是這個意圖,但是很難在這種非常錯誤的代碼中看到任何東西。 – AnT 2010-09-01 20:14:50
我在末尾修改了我的帖子 – Mike 2010-09-01 20:22:19