Installation and use of the C program memory leak detection tool Valgrind under Unix

  • 2020-04-02 01:21:05
  • OfStack

Valgrind is a software development tool for memory debugging, memory leak detection, and performance analysis.
The original author of Valgrind was Julian Seward, who won the second annual google-o 'reilly open source award in 2006 for his work on developing Valgrind.
Valgrind is free software under the terms of the GNU general public license.

website
(link: http://www.valgrind.org/)  
Download and install
Wget # http://www.valgrind.org/downloads/ (link: http://xiazai.jb51.net/201308/yuanma/valgrind-3.8.1 (jb51.net). Rar) # tar XVF valgrind - 3.8.1. Tar..bz2
# CD valgrind - 3.8.1
#. / configure -- prefix = / usr/local/webserver/valgrind
# make
# make install

The test code

#include <stdlib.h>
int* func(void)
{
   int* x = malloc(10 * sizeof(int));
   x[10] = 0;  //Problem 1: array index overbounds
}                  
 int main(void)
{
   int* x=NULL;
   x=func();
   //free(x);  
   x=NULL;
   return 0;   //Problem 2: memory is not free
 }

compile
Gcc-g-o test test.c

Check the memory
# valgrind -- tool = memcheck - leak - check = yes - show - reachable = yes. / test

Report:
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201308/201308130856492.jpg ">

instructions
Invalid write of size 4: indicates that the array was overwritten by 4 bytes
40 bytes in 1 blocks: represents a memory leak of 40 bytes due to program exit

Fixed bug, rechecked prompt no memory leak

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201308/201308130856493.jpg ">

Documents:
The Memcheck tool included with Valgrind checks for the following program errors:

Use of uninitialised memory
Reading/writing memory after it has been free'd
Use more memory than malloc allocated (Reading/writing off the end of malloc'd blocks)
Illegal access to Reading/writing areas on the stack
(Memory leaks, where Pointers to malloc'd blocks are lost forever)
Mismatched use of malloc/new/new [] vs free/delete/delete [])
SRC and DST overlap (Overlapping SRC and DST Pointers in memcpy () and related functions provides)
Repeat free


Related articles: