锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 綆浠嬶細(xì) Valgrind鏄竴嬈劇敤浜庡唴瀛樿皟璇曘?a >鍐呭瓨娉勬紡媯(gè)嫻嬩互鍙?a >鎬ц兘鍒嗘瀽鐨?a >杞歡寮鍙戝伐鍏?/a>銆?/p> 浣跨敤鏂規(guī)硶錛?/strong> vargrind --tool=memcheck ./a.out 濡傛灉闇瑕佹棩蹇楋紝鍒欐坊鍔?/p> vargrind 鈥搇og-file=var.log 鈥搕ool=memcheck ./a.out 甯哥敤鐨刼ptions錛?鈥搕rack-origins=yes 鈥搇eak-check=full 鈥搉um-callers=* 1.鍐呭瓨娉勯湶錛?/p> 鐩稿叧鏃ュ織錛?/p>
==20220== Memcheck, a memory error detector 2.浣跨敤鏈垵濮嬪寲鐨勫唴瀛?/p> 鏃ュ織鍒嗘瀽錛?/p>
==20345== Memcheck, a memory error detector 鍙互浣跨敤--track-origins=yes 寰楀埌鏇村鐨勪俊鎭?/p>
3.鍐呭瓨璇誨啓瓚婄晫 ==20368== Memcheck, a memory error detector 4.鍐呭瓨鐢寵閲婃斁綆$悊閿欒 ==20387== Memcheck, a memory error detector 榪欓噷鍙槸綆鍗曠殑嫻嬭瘯浜?jiǎn)鍑犱釜渚嬪瓙锛屾湁鍏喘懀鐨勮鑰呭彲浠ュ弬鑰冧笅闈㈢殑url銆?/p>
鍙傝冿細(xì) http://www.ibm.com/developerworks/cn/linux/l-cn-valgrind/#include <stdio.h>
void function()
{
int *p = (int*)malloc(10*sizeof(int));
p[10] = 0;
}
int main()
{
function();
return 0;
}
==20220== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==20220== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==20220== Command: ./test
==20220== Parent PID: 20160
==20220==
==20220== Invalid write of size 4
==20220== at 0x80483FF: function (in /mnt/Documents/Training/valgrind/test)
==20220== by 0x8048411: main (in /mnt/Documents/Training/valgrind/test)
==20220== Address 0x41be050 is 0 bytes after a block of size 40 alloc'd
==20220== at 0x4028876: malloc (vg_replace_malloc.c:236)
==20220== by 0x80483F5: function (in /mnt/Documents/Training/valgrind/test)
==20220== by 0x8048411: main (in /mnt/Documents/Training/valgrind/test)
==20220==
==20220==
==20220== HEAP SUMMARY:
==20220== in use at exit: 40 bytes in 1 blocks
==20220== total heap usage: 1 allocs, 0 frees, 40 bytes allocated
==20220==
==20220== LEAK SUMMARY:
==20220== definitely lost: 40 bytes in 1 blocks
==20220== indirectly lost: 0 bytes in 0 blocks
==20220== possibly lost: 0 bytes in 0 blocks
==20220== still reachable: 0 bytes in 0 blocks
==20220== suppressed: 0 bytes in 0 blocks
==20220== Rerun with --leak-check=full to see details of leaked memory
==20220==
==20220== For counts of detected and suppressed errors, rerun with: -v
==20220== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 11 from 6)#include <stdio.h>
int main()
{
int a;
if (a==1)
{
printf("a==%d\n",a);
}
return 0;
}
==20345== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==20345== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==20345== Command: ./test
==20345==
==20345== Conditional jump or move depends on uninitialised value(s)
==20345== at 0x80483F2: main (in /mnt/Documents/Training/valgrind/test)
==20345==
==20345==
==20345== HEAP SUMMARY:
==20345== in use at exit: 0 bytes in 0 blocks
==20345== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==20345==
==20345== All heap blocks were freed -- no leaks are possible
==20345==
==20345== For counts of detected and suppressed errors, rerun with: -v
==20345== Use --track-origins=yes to see where uninitialised values come from
==20345== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 11 from 6)#include <stdio.h>
int main()
{
int *a = (int*)malloc(5*sizeof(int));
a[5] = 1;
return 0;
}
==20368== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==20368== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==20368== Command: ./test
==20368==
==20368== Invalid write of size 4
==20368== at 0x8048404: main (in /mnt/Documents/Training/valgrind/test)
==20368== Address 0x41be03c is 0 bytes after a block of size 20 alloc'd
==20368== at 0x4028876: malloc (vg_replace_malloc.c:236)
==20368== by 0x80483F8: main (in /mnt/Documents/Training/valgrind/test)
==20368==
==20368==
==20368== HEAP SUMMARY:
==20368== in use at exit: 20 bytes in 1 blocks
==20368== total heap usage: 1 allocs, 0 frees, 20 bytes allocated
==20368==
==20368== LEAK SUMMARY:
==20368== definitely lost: 20 bytes in 1 blocks
==20368== indirectly lost: 0 bytes in 0 blocks
==20368== possibly lost: 0 bytes in 0 blocks
==20368== still reachable: 0 bytes in 0 blocks
==20368== suppressed: 0 bytes in 0 blocks
==20368== Rerun with --leak-check=full to see details of leaked memory
==20368==
==20368== For counts of detected and suppressed errors, rerun with: -v
==20368== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 11 from 6)#include <stdio.h>
int main()
{
int *a = new int[5];
/*free(a);*/
delete a;
return 0;
}
==20387== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==20387== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==20387== Command: ./test
==20387==
==20387== Mismatched free() / delete / delete []
==20387== at 0x4027919: operator delete(void*) (vg_replace_malloc.c:387)
==20387== by 0x8048498: main (in /mnt/Documents/Training/valgrind/test)
==20387== Address 0x42f2028 is 0 bytes inside a block of size 20 alloc'd
==20387== at 0x4027F65: operator new[](unsigned int) (vg_replace_malloc.c:299)
==20387== by 0x8048488: main (in /mnt/Documents/Training/valgrind/test)
==20387==
==20387==
==20387== HEAP SUMMARY:
==20387== in use at exit: 0 bytes in 0 blocks
==20387== total heap usage: 1 allocs, 1 frees, 20 bytes allocated
==20387==
==20387== All heap blocks were freed -- no leaks are possible
==20387==
==20387== For counts of detected and suppressed errors, rerun with: -v
==20387== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 17 from 6)
]]>