首页 > 解决方案 > Valgrind:数学函数中的“条件跳转或移动取决于未初始化的值”

问题描述

我在我的 C++ 程序中找不到导致条件跳转的原因。我正在使用 Valgrind 3.11.0 对其进行测试,并且该程序是使用 gcc 5.4.0 编译的。

问题是这些条件跳转似乎出现在使用 AVX 指令的三角函数中,但也出现在sqrt()函数中,而不是在我的代码中。下面我粘贴 Valgrind 消息:

==29490== Conditional jump or move depends on uninitialised value(s)
==29490==    at 0x54436DD: __sin_avx (s_sin.c:482)
...

==29490== Conditional jump or move depends on uninitialised value(s)
==29490==    at 0x54449D4: __cos_avx (s_sin.c:597)
...
==28458== Conditional jump or move depends on uninitialised value(s)
==28458==    at 0x543F7B7: __ieee754_atan2_avx (e_atan2.c:434)
==28458==    by 0x53F746F: atan2 (w_atan2.c:36)
...

==29490== Conditional jump or move depends on uninitialised value(s)
==29490==    at 0x53F8258: sqrt (w_sqrt.c:27)

我希望有人能帮帮忙。谢谢。

标签: c++valgrind

解决方案


类似atan2的函数在内部将参数与一些常量进行比较(例如,if (x < 0) ...)。如果你传递的是一个未初始化的值,valgrind 会正确地抱怨(例如尝试double x, y; atan2(x, y);)。问题不在于数学函数,而在于调用者。

您需要确保参数已正确初始化,这可能很重要,因为未初始化的值可能会从意想不到的地方传播。尝试使用调试符号编译代码,看看 valgrind 是否可以打印更详细的回溯。


推荐阅读