关于valgrind的 “Conditional jump or move depends on uninitialised value(s)”:
valgrind在报这个错误的时候程序未必有bug,如下例:
执行:
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.
valgrind在报这个错误的时候程序未必有bug,如下例:
代码
//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 *p = new char [10];
if(ptr(p)){
printf("%s\n",p);
}
delete [] p;
return 0;
}
#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 *p = 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.
因此在遇到这种错误的时候需要仔细分析。