Valgrind 简单实用(转载+修正) 规格严格

#include <iostream>

using namespace std;

//mytest.cpp
int main(int argc, char * argv[])
//line 120
 const int N=10;              // # of elements in array
 const int g_nLargeRange = 500 * 1024 * 1024;

 cout 
<< "Start of tests" << endl;
 
int *p1 = new int(1);      // use to cause leak
 int *p2 = new int[N];      // allocate an int array
 int *p3 = new int(2);      // used to test wrong delete
 char *cp = 0;              // cp is null pointer
 char ca[3];                // unintialized array
 char * pLarge = NULL;    // used to test set address range perms: large range
 cout << "Test 1: off by one" << endl;
 
for (int i=1; i<N+1; i++)  // one-off in loop
  p2[i] = i;               // err - initialize element p[N]
 cout << "Test 2: access freed storage" << endl;
 delete p1;
 
*p1 = 3;                   // err - accessing freed storage
 cout << "Test 3: using uninitialized storage" << endl;
 
if (p2[0]) cout << "Junk" << endl;// err - used uninit data
 cout << "Test 4: delete array using scalar delete" << endl;
 delete p2;                 
// err - delete array with scalar delete
 cout << "Test 5: array delete of scalar" << endl;
 delete [] p3;              
// err - array delete of scalar
 cout << "Test 6: overlapping storage blocks" << endl;
 memcpy( ca, 
&ca[1],2 );    // err - overlapping storage blocks
 cout << "Test 7: system call using uninitialize data" << endl;
 sleep( 
1 & ca[0] );            // err - uninit data in system call
 cout << "Test 8: set address range perms: large range" << endl;
 pLarge 
= new char[g_nLargeRange];
 cout 
<< "Test 9: assign to null pointer - seg faults" << endl;
 
*cp = 'a';                 // err - used null pointer (Seg fauilts)
 cout << "End of tests" << endl;
 
return 0;
}

运行 g++ -o a a.cpp

valgrind --tool=memcheck --num-callers=50 --leak-check=full  --log-file=memcheck ./a

屏幕输出

Start of tests
Test 
1: off by one
Test 
2: access freed storage
Test 
3using uninitialized storage
Test 
4: delete array using scalar delete
Test 
5: array delete of scalar
Test 
6: overlapping storage blocks
Test 
7: system call using uninitialize data
Test 
8set address range perms: large range
Test 
9: assign to null pointer - seg faults
段错误

日志

==29816== Memcheck, a memory error detector.
==29816== Copyright (C) 2002-2008, and GNU GPL'd, by Julian Seward et al.
==29816== Using LibVEX rev 1884, a library for dynamic binary translation.
==29816== Copyright (C) 2004-2008, and GNU GPL'd, by OpenWorks LLP.
==29816== Using valgrind-3.4.1, a dynamic binary instrumentation framework.
==29816== Copyright (C) 2000-2008, and GNU GPL'd, by Julian Seward et al.
==29816== For more details, rerun with: -v
==29816==
==29816== My PID = 29816, parent PID = 26483.  Prog and args are:
==29816==    ./a
==29816==
==29816== Invalid write of size 4
==29816==    at 0x804887B: main (a.cpp:20)
==29816==  Address 0x401c088 is 0 bytes after a block of size 40 alloc'd
==29816==    at 0x4005515: operator new[](unsigned int) (vg_replace_malloc.c:268)
==29816==    by 0x8048819: main (a.cpp:13)
==29816==
==29816== Invalid write of size 4
==29816==    at 0x80488B9: main (a.cpp:23)
==29816==  Address 0x401c028 is 0 bytes inside a block of size 4 free'd
==29816==    at 0x4005BD1: operator delete(void*) (vg_replace_malloc.c:342)
==29816==    by 0x80488B5: main (a.cpp:22)
==29816==
==29816== Conditional jump or move depends on uninitialised value(s)
==29816==    at 0x80488EA: main (a.cpp:25)
==29816==
==29816== Mismatched free() / delete / delete []
==29816==    at 0x4005BD1: operator delete(void*) (vg_replace_malloc.c:342)
==29816==    by 0x804893E: main (a.cpp:27)
==29816==  Address 0x401c060 is 0 bytes inside a block of size 40 alloc'd
==29816==    at 0x4005515: operator new[](unsigned int) (vg_replace_malloc.c:268)
==29816==    by 0x8048819: main (a.cpp:13)
==29816==
==29816== Mismatched free() / delete / delete []
==29816==    at 0x4005F61: operator delete[](void*) (vg_replace_malloc.c:364)
==29816==    by 0x8048973: main (a.cpp:29)
==29816==  Address 0x401c0b8 is 0 bytes inside a block of size 4 alloc'd
==29816==    at 0x400502D: operator new(unsigned int) (vg_replace_malloc.c:224)
==29816==    by 0x8048828: main (a.cpp:14)
==29816==
==29816== Conditional jump or move depends on uninitialised value(s)
==29816==    at 0xA8F7AC: sleep (in /lib/libc-2.5.so)
==29816==    by 0x80489DC: main (a.cpp:33)
==29816== Warning: set address range perms: large range [0x884a028, 0x27c4a028) (undefined)
==29816==
==29816== Invalid write of size 1
==29816==    at 0x8048A37: main (a.cpp:37)
==29816==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==29816==
==29816== Process terminating with default action of signal 11 (SIGSEGV)
==29816==  Access not within mapped region at address 0x0
==29816==    at 0x8048A37: main (a.cpp:37)
==29816==  If you believe this happened as a result of a stack overflow in your
==29816==  program's main thread (unlikely but possible), you can try to increase
==29816==  the size of the main thread stack using the --main-stacksize= flag.
==29816==  The main thread stack size used in this run was 10485760.
==29816==
==29816== ERROR SUMMARY: 7 errors from 7 contexts (suppressed: 15 from 1)
==29816== malloc/free: in use at exit: 524,288,000 bytes in 1 blocks.
==29816== malloc/free: 4 allocs, 3 frees, 524,288,048 bytes allocated.
==29816== For counts of detected errors, rerun with: -v
==29816== Use --track-origins=yes to see where uninitialised values come from
==29816== searching for pointers to 1 not-freed blocks.
==29816== checked 87,856 bytes.
==29816==
==29816== LEAK SUMMARY:
==29816==    definitely lost: 0 bytes in 0 blocks.
==29816==      possibly lost: 0 bytes in 0 blocks.
==29816==    still reachable: 524,288,000 bytes in 1 blocks.
==29816==         suppressed: 0 bytes in 0 blocks.
==29816== Reachable blocks (those to which a pointer was found) are not shown.
==29816== To see them, rerun with: --leak-check=full --show-reachable=yes
原文地址:https://www.cnblogs.com/diyunpeng/p/2071298.html