继续介绍valgrind的用途, 看程序:

#include <stdio.h>
#include <string.h>

int main()
{
	int i;
	if(i == 0)
	{
		printf("[%d]\n", i);
	}

	return 0;
}
      一眼就能看出程序的问题, valgrind分析如下:

[root@xxx ~/valgrind-3.8.1/bin]# g++ -g test.cpp
[root@xxx ~/valgrind-3.8.1/bin]# 
[root@xxx ~/valgrind-3.8.1/bin]# ./valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./a.out
==8088== Memcheck, a memory error detector
==8088== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==8088== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==8088== Command: ./a.out
==8088== 
==8088== Conditional jump or move depends on uninitialised value(s)
==8088==    at 0x4005B0: main (test.cpp:7)
==8088== 
==8088== Use of uninitialised value of size 8
==8088==    at 0x5612A9B: _itoa_word (in /lib64/libc-2.12.so)
==8088==    by 0x5615652: vfprintf (in /lib64/libc-2.12.so)
==8088==    by 0x561E189: printf (in /lib64/libc-2.12.so)
==8088==    by 0x4005C5: main (test.cpp:9)
==8088== 
==8088== Conditional jump or move depends on uninitialised value(s)
==8088==    at 0x5612AA5: _itoa_word (in /lib64/libc-2.12.so)
==8088==    by 0x5615652: vfprintf (in /lib64/libc-2.12.so)
==8088==    by 0x561E189: printf (in /lib64/libc-2.12.so)
==8088==    by 0x4005C5: main (test.cpp:9)
==8088== 
==8088== Conditional jump or move depends on uninitialised value(s)
==8088==    at 0x56140E3: vfprintf (in /lib64/libc-2.12.so)
==8088==    by 0x561E189: printf (in /lib64/libc-2.12.so)
==8088==    by 0x4005C5: main (test.cpp:9)
==8088== 
==8088== Conditional jump or move depends on uninitialised value(s)
==8088==    at 0x5614101: vfprintf (in /lib64/libc-2.12.so)
==8088==    by 0x561E189: printf (in /lib64/libc-2.12.so)
==8088==    by 0x4005C5: main (test.cpp:9)
==8088== 
[0]
==8088== 
==8088== HEAP SUMMARY:
==8088==     in use at exit: 0 bytes in 0 blocks
==8088==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==8088== 
==8088== All heap blocks were freed -- no leaks are possible
==8088== 
==8088== For counts of detected and suppressed errors, rerun with: -v
==8088== Use --track-origins=yes to see where uninitialised values come from
==8088== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 6 from 6)
[root@xxx ~/valgrind-3.8.1/bin]# 
      问题显而易见。






本文转载:CSDN博客