Monday, December 19, 2016

How to use Valgrind for debugging C/C++ program


Valgrind tool is used to for memory leaks, uninitialized memory, reading or writing to memory that has been freed, invalid access to memory(boundary conditions for the dynamic arrays), mismatched use of malloc/new/new[] with free/delete/delete[] , double free/delete.

How to use Valgrind:
>Compile the program with -g option (this option will add debugging symbols), so that we can see line numbers of the program when debugging

 $ g++ -o test -g test.cpp  

>use valgrind command to analyze

 $ valgrind --tool=memcheck --leak-check=yes ./test

Example: test.cpp
The following example demonstrates the debugging C++ program for
  • memory leak when the memory allocated with new don’t find delete
  • mismatched new/new[] with delete/delete[] operators. When we create heap memory for arrays of objects/variables we use [], and when delete also we have to use []
  • Boundary conditions for the allocated memory(index out of range).
  • uninitialised variable when used in the conditions.
 #include<iostream>  
 using namespace std;  
 int main()   
 {  
  int i;  
  char *c = new char;  //no delete for this memory allocated  
  char *s = new char[10];   
  s[10] = 'w'; //Invalid access  
  if(i) { //Uninitialised variable i  
  cout<<"Hello ";  
  }  
  delete s; //mis matched delete  
  return 0;  
 }  

when you compile above program and run valgrind tool the following result will come

$ g++ -o test -g test.cpp
$valgrind --tool=memcheck --leak-check=yes  ./test
 ==5455== Memcheck, a memory error detector  
 ==5455== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.  
 ==5455== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info  
 ==5455== Command: ./test  
 ==5455==   
 ==5455== Invalid write of size 1  
 ==5455==  at 0x8048634: main (test.cpp:11)  
 ==5455== Address 0x432906a is 0 bytes after a block of size 10 alloc'd  
 ==5455==  at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)  
 ==5455==  by 0x8048628: main (test.cpp:10)  
 ==5455==   
 ==5455== Conditional jump or move depends on uninitialised value(s)  
 ==5455==  at 0x804863C: main (test.cpp:13)  
 ==5455==   
 ==5455== Mismatched free() / delete / delete []  
 ==5455==  at 0x402ACFC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)  
 ==5455==  by 0x804865D: main (test.cpp:17)  
 ==5455== Address 0x4329060 is 0 bytes inside a block of size 10 alloc'd  
 ==5455==  at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)  
 ==5455==  by 0x8048628: main (test.cpp:10)  
 ==5455==   
 Hello ==5455==   
 ==5455== HEAP SUMMARY:  
 ==5455==   in use at exit: 1 bytes in 1 blocks  
 ==5455==  total heap usage: 2 allocs, 1 frees, 11 bytes allocated  
 ==5455==   
 ==5455== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1  
 ==5455==  at 0x402B9B4: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)  
 ==5455==  by 0x8048618: main (test.cpp:8)  
 ==5455==   
 ==5455== LEAK SUMMARY:  
 ==5455==  definitely lost: 1 bytes in 1 blocks  
 ==5455==  indirectly lost: 0 bytes in 0 blocks  
 ==5455==   possibly lost: 0 bytes in 0 blocks  
 ==5455==  still reachable: 0 bytes in 0 blocks  
 ==5455==     suppressed: 0 bytes in 0 blocks  
 ==5455==   
 ==5455== For counts of detected and suppressed errors, rerun with: -v  
 ==5455== Use --track-origins=yes to see where uninitialised values come from  
 ==5455== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)  

 
>Corrected Version of the above program: The following program is the modified version of the above program with all the above problems corrected.

 #include<iostream>  
 using namespace std;  
 int main()   
 {  
  int i=1;  
  char *c = new char;    
  char *s = new char[10];   
  s[9] = 'w';   
  if(i) {   
  cout<<"Hello ";  
  }  
  delete c;  
  delete s[];   
  return 0;  
 }  


PS: valgrind will not check for the invalid access(boundary conditions) of arrays allocated in stack.



No comments:

Post a Comment