【发布时间】:2015-07-30 10:31:57
【问题描述】:
我试图了解这段代码有什么问题:
int myCompare( const void * v1, const void * v2 ){
const int * f1 = static_cast<const int *>(v1);
const int * f2 = static_cast<const int *>(v2);
if( f1[ 0 ] < f2[ 0 ] ) return -1;
if( f1[ 0 ] > f2[ 0 ] ) return +1;
return 0;
}
这是 qsort 的比较器函数,它工作正常,但是当我使用 valgrind 对其进行分析时,我在此字符串中显示此消息 条件跳转或移动取决于未初始化的值
if( f1[ 0 ] < f2[ 0 ] ) return -1;
if( f1[ 0 ] > f2[ 0 ] ) return +1;
qsort 正在从here 调用
valgrind 输出
==25053== Conditional jump or move depends on uninitialised value(s)
==25053== at 0x66474CB: msort_with_tmp.part.0 (msort.c:83)
==25053== by 0x6647221: msort_with_tmp.part.0 (msort.c:45)
==25053== by 0x6647221: msort_with_tmp.part.0 (msort.c:45)
==25053== by 0x664777B: qsort_r (msort.c:45)
==25053== by 0x40D002: owOpenCLSolver::_runSort(owConfigProrerty*)(owOpenCLSolver.cpp:473)
==25053== by 0x4114DB: owPhysicsFluidSimulator::simulationStep(bool) (owPhysicsFluidSimulator.cpp:186)
==25053== by 0x412FBC: run(int, char**, bool) (owWorldSimulation.cpp:919)
==25053== by 0x4058D4: main (main.cpp:95)
==25053== Uninitialised value was created by a client request
==25053== at 0xA6AF9F8: ??? (in /usr/lib/libamdocl64.so)
==25053== by 0xA69BF40: ??? (in /usr/lib/libamdocl64.so)
==25053== by 0xA3DCB0F: ??? (in /usr/lib/libamdocl64.so)
==25053== by 0xA42DECA: ??? (in /usr/lib/libamdocl64.so)
==25053== by 0xA37E03F: ??? (in /usr/lib/libamdocl64.so)
==25053== by 0xA3558D0: ??? (in /usr/lib/libamdocl64.so)enter code here
==25053== by 0xA35A1C1: ??? (in /usr/lib/libamdocl64.so)
==25053== by 0xA35A2EC: ??? (in /usr/lib/libamdocl64.so)
==25053== by 0xA32972D: ??? (in /usr/lib/libamdocl64.so)
==25053== by 0xA2D095E: ??? (in /usr/lib/libamdocl64.so)
==25053== by 0xA337AEB: ??? (in /usr/lib/libamdocl64.so)
==25053== by 0x6BDD181: start_thread (pthread_create.c:312)
更新
实际上,在我使用此排序函数的类 owOpenCLSolver 中,我没有任何地方可以显式初始化缓冲区 _particleIndex(将要排序的缓冲区),但初始化正在函数 copy_buffer_from_device 中进行,该函数正在从 OpenCL 设备复制数据到主机程序缓冲区。所以问题可能在于 valgrind 在函数使用它时并不认为 _particleIndex 不是 init。
【问题讨论】:
-
你为什么首先使用
qsort而不是std::sort? -
你确定,这个函数不是用未初始化的参数调用的吗?
-
@MohitJain 我正在尝试对数组进行排序,我看到 std::sort 与迭代器一起使用。实际上我没有考虑std::sort,所以我不能告诉你为什么:)。但是感谢您提供的信息,我会更准确地查看它
-
@πάνταῥεῖ 实际上我不确定,我只能假设代码工作正常来判断
-
@segvara
int arr1[] = {4, 1, 3, 2}; std::sort(std::begin(arr1), std::end(arr1));或int arr1[] = {4, 1, 3, 2}; std::sort(&arr1[0], &arr1[4]);
标签: c++ memory-leaks profiling valgrind