Valgrind trial (1)
InstallProgram
Sudo apt-Get install valgrind
Edit Test Program
Gedit test. cpp
# Include <string. h>
# Include <stdlib. h>
Int main (INT argc, char * argv [])
{
Char * PTR;
PTR = (char *) malloc (10 );
Strcpy (PTR, "01234567890 ");
Return 0;
}
Compile and run
Cutepig @ Ubuntu :~ /Testvalgrind $ g ++-g-o Test test. cpp
Cutepig @ Ubuntu :~ /Testvalgrind $ valgrind -- tool = memcheck -- leak-check = yes./test
==13038 = memcheck, a memory error detector
= 13038 = copyright (c) 2002-2009, and gnu gpl 'd, by Julian Seward et al.
= 13038 = Using Valgrind-3.5.0-Debian and libvex; rerun with-H for copyright info
= 13038 = command:./test
= 13038 =
==13038 = Invalid write of size 1
= 13038 = at 0x4026195: memcpy (mc_replace_strmem.c: 482)
= 13038 = by 0x8048538: Main (test. cpp: 10)
= 13038 = address 0x42b6032 is 0 bytes after a block of size 10 alloc 'd
= 13038 = at 0x4024c1c: malloc (vg_replace_malloc.c: 195)
= 13038 = by 0x8048518: Main (test. cpp: 9)
= 13038 =
==13038 = Invalid write of size 1
= 13038 = at 0x402619e: memcpy (mc_replace_strmem.c: 482)
= 13038 = by 0x8048538: Main (test. cpp: 10)
= 13038 = address 0x42b6033 is 1 bytes after a block of size 10 alloc 'd
= 13038 = at 0x4024c1c: malloc (vg_replace_malloc.c: 195)
= 13038 = by 0x8048518: Main (test. cpp: 9)
= 13038 =
= 13038 =
==13038 === heap summary:
= 13038 = in use at Exit: 10 bytes in 1 blocks
= 13038 = total heap usage: 1 allocs, 0 frees, 10 bytes allocated
= 13038 =
==13038 = 10 bytes in 1 blocks are definitely lost in loss record 1 of 1
= 13038 = at 0x4024c1c: malloc (vg_replace_malloc.c: 195)
= 13038 = by 0x8048518: Main (test. cpp: 9)
= 13038 =
= 13038 = leak summary:
= 13038 = definitely lost: 10 bytes in 1 blocks
==13038 = indirectly lost: 0 bytes in 0 blocks
==13038 = possibly lost: 0 bytes in 0 blocks
= 13038 = still reachable: 0 bytes in 0 blocks
= 13038 = suppressed: 0 bytes in 0 blocks
= 13038 =
= 13038 = for counts of detected and suppressed errors, Rerun with:-V
= 13038 = Error Summary: 3 errors from 3 contexts (suppressed: 18 from 7)
Check to cross-border, located in main (test. cpp: 10), leaked, located in main (test. cpp: 9)
Write another normal program
Cutepig @ Ubuntu :~ /Testvalgrind $ gedit test2.cpp
# Include <string. h>
# Include <stdlib. h>
Int main (INT argc, char * argv [])
{
Char * PTR;
PTR = (char *) malloc (100 );
Strcpy (PTR, "01234567890 ");
Free (PTR );
Return 0;
}
Cutepig @ Ubuntu :~ /Testvalgrind $ g ++ test2.cpp
Cutepig @ Ubuntu :~ /Testvalgrind $ valgrind -- tool = memcheck -- leak-check = yes./A. Out
==12949 = memcheck, a memory error detector
= 12949 = copyright (c) 2002-2009, and gnu gpl 'd, by Julian Seward et al.
= 12949 = Using Valgrind-3.5.0-Debian and libvex; rerun with-H for copyright info
= 12949 = command:./A. Out
= 12949 =
= 12949 =
==12949 === heap summary:
= 12949 = in use at Exit: 0 bytes in 0 blocks
= 12949 = total heap usage: 1 allocs, 1 frees, 100 bytes allocated
= 12949 =
= 12949 = all heap blocks were freed -- no leaks are possible
= 12949 =
= 12949 = for counts of detected and suppressed errors, Rerun with:-V
= 12949 = Error Summary: 0 errors from 0 contexts (suppressed: 18 from 7)
OK, no memory problems
Http://www.cnblogs.com/cutepig/archive/2009/08/02/1537180.html