关于valgrind的 “Conditional jump or move depends on uninitialised value(s)”

本文探讨了Valgrind在特定条件下可能产生的误报问题,通过一个实例代码演示了即使存在Conditional jump or move depends on uninitialised value(s)警告,程序也可能正常运行且无实际bug的情况。文章强调了在遇到此类警告时,开发者需要仔细分析代码逻辑,判断警告是否真正指向潜在的问题。
关于valgrind的 “Conditional jump or move depends on uninitialised value(s)”:

valgrind在报这个错误的时候程序未必有bug,如下例:

ExpandedBlockStart.gif代码
//test.cpp
#include <iostream>
using namespace std;
bool ptr(char *testptr)
{
        
int i=0;
        
if(i == 0){
                memcpy(testptr,
"Hello",5);
                
return true;
        }
        
return false;
}

int main(int argc, const char *argv[])
{
        
char *= new char [10];
        
if(ptr(p)){
                printf(
"%s\n",p);
        }

        delete [] p;

        
return 0;
}



执行:
g++ test.cpp -Wall
valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./a.out

就会报下面的错:

==8472==
==8472== Conditional jump or move depends on uninitialised value(s)
==8472==    at 0x4906507: strlen (mac_replace_strmem.c:243)
==8472==    by 0x3D26142A0A: vfprintf (in /lib64/tls/libc-2.3.4.so)
==8472==    by 0x3D26148157: printf (in /lib64/tls/libc-2.3.4.so)
==8472==    by 0x4009A6: main (in /home/maoqi/private/cpp/a.out)
Hello
==8472==
==8472== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 5 from 2)
==8472== malloc/free: in use at exit: 0 bytes in 0 blocks.
==8472== malloc/free: 1 allocs, 1 frees, 10 bytes allocated.
==8472== For counts of detected errors, rerun with: -v
==8472== All heap blocks were freed -- no leaks are possible.

因此在遇到这种错误的时候需要仔细分析。

 

 

转载于:https://www.cnblogs.com/FrankTan/archive/2010/01/08/1642569.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值