C语言 条件跳转或移动取决于未初始化的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3621657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conditional jump or move depends on uninitialised value(s)
提问by Mike
i'm breaking my head for hours on the following problem: i pasted 2 functions althought there are many more. i run valgrind on my program and i get 32 errors similar to this:
我在以下问题上头疼了好几个小时:我粘贴了 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)
i get more errors on other functions, however it's same error. i can't understand why it is uninitialized. Thanks everyone for your help.
我在其他功能上遇到更多错误,但它是相同的错误。我不明白为什么它是未初始化的。感谢大家的帮助。
this is the main function:
这是主要功能:
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;
}
these defs are in a header file:
这些定义位于头文件中:
typedef struct Object* ObjectP;
typedef struct Table* TableP;
typedef const struct Table* ConstTableP;
typedef enum {FALSE, TRUE} Boolean;
this code is in another file:
此代码在另一个文件中:
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;
this function inserts a key to a hashtable. if 3 keys are already chained in the cell then size of the table is doubled and i'm doing some other things in doubleTable()...
此函数向哈希表插入一个键。如果 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;
}
EDIT:
编辑:
i ran valgrind as you said and i got:
我按照你说的运行了 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)
i have this method:
我有这个方法:
static ObjectP* allocateArray(int tableSize) {
objectP* arr = (ObjectP*)malloc(tableSize * sizeof(ObjectP));
return arr;
}
this creates an array of pointers, which I never initialized. could this be the problem? and how i should initialize a pointers array? to NULL?
这将创建一个我从未初始化的指针数组。这可能是问题吗?我应该如何初始化一个指针数组?为NULL?
回答by arsenm
You need to run valgrind with the --track-origins=yesoption to find the origins of undefined values.
您需要使用--track-origins=yes选项运行 valgrind以查找未定义值的来源。
回答by Mike
The problem was that I didn't initialise the pointers array when I created it.
问题是我在创建指针数组时没有初始化它。
回答by Jens Gustedt
It looks to me that you didn't compile your program with a debugging flag (-gfor gcc). Then if you run valgrind with all options on it should tell you exactly which variables cause the problem.
在我看来,您没有使用调试标志(-g对于 gcc)编译您的程序。然后,如果您运行带有所有选项的 valgrind,它应该会准确地告诉您哪些变量会导致问题。
回答by John Kugelman
Where do you initialize table->_table? Check that you are properly initializing it in, I presume, CreateTable(). Please post the code for that function if nothing obvious pops out at you.
你在哪里初始化table->_table?检查您是否正确初始化它,我想,CreateTable()。如果没有任何明显的东西弹出,请发布该函数的代码。
回答by Subham Debnath
Simple - just run
简单 - 只需运行
valgrind --tool=memcheck --track-origins=yes <program_path>
...to find out.
……去了解一下。

