C Program memory leak Detection Tool in UNIX Valgrind installation and Usage _c language

Source: Internet
Author: User
Tags bz2 valgrind
Valgrind is a software development tool for memory debugging, memory leak detection, and performance analysis.
Valgrind's original author was Julian Seward, who won the second Google-o ' Reilly Open source Code award in 2006 for his work on the development of Valgrind.
Valgrind complies with the GNU General Public License Terms and is a free software.

official website
http://www.valgrind.org

Download and install
#wget http://www.valgrind.org/downloads/ valgrind-3.8.1.tar.bz2
#tar XVF valgrind-3.8.1.tar.bz2
#cd valgrind-3.8.1
#./configure--prefix=/usr/local/webserver/valgrind
#make
#make Install

Test Code
Copy Code code as follows:

#include <stdlib.h>
int* func (void)
{
int* x = malloc (A * sizeof (int));
X[10] = 0; Problem 1: Array subscript out of bounds
}
int main (void)
{
int* X=null;
X=func ();
Free (x);
X=null;
return 0; Issue 2: Memory not released
}

compiling
#gcc-G-O test test.c

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

Report:


description
Invalid Write of size 4:4 bytes in array out of bounds
Bytes in 1 blocks: Indicates a memory leak 40 bytes due to program exit

Fix bugs, re-check hints, no memory leaks



Document:
The Memcheck tool contained in Valgrind can check the following program errors:

Using uninitialized memory (use of uninitialised memory)
Use memory that has been freed (reading/writing memory after it has been free ' d)
Use more than malloc allocated memory space (reading/writing off the end of malloc ' d blocks)
Illegal access to stack (reading/writing inappropriate areas on the stack)
Whether the requested space is released (Memory leaks–where pointers to malloc ' d blocks are lost)
Malloc/free/new/delete application and free memory matching (mismatched use of malloc/new/new [] vs Free/delete/delete [])
Overlap between src and DST (overlapping SRC and DST pointers in memcpy () and related functions)
Repeat free

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.