gpt4 book ai didi

有条件的跳跃或移动取决于未初始化的值

转载 作者:太空狗 更新时间:2023-10-29 16:33:10 25 4
gpt4 key购买 nike

<分区>

我为以下问题伤脑筋好几个小时:我粘贴了 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;
}

这些定义在头文件中:

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?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com