How to detect memory leakage in C program?
One more tips for c-programmer,embedded software engineers.
- In many case you need to make sure that your programs is not going to leak any heap memory.
- In Manny case while debugging code you will find SEGMENTATION FAULT and you cant debug your code ahead.
So in that all case our best friend is Valgrind
Introduction:
The Valgrind tool suite provides a number of debugging and profiling tools that help you make your programs faster
and more correct. The most popular of these tools is called Memcheck. It can detect many memory-related errors
that are common in C and C++ programs and that can lead to crashes and unpredictable behaviour.
Download & install :
You can download this tool for For {x86,amd64,arm,ppc32,ppc64}-linux and {x86,amd64}-darwin (Mac OS X). from here.
After getting this whole package configure it & install it in your system.
Use of Valgrind :
1> Compile your program with -g to include debugging information so that Memcheck’s error messages include exact
line numbers. like
gcc -g shareprogrammingtips.c
2> Now If you normally run your program like this:
./a.out arg1 arg2
Use this command line:
valgrind –leak-check=yes ./a.out arg1 arg2
3>Interpreting Memcheck’s output
now try to interpreat its output. Memcheck will shows where you have done such mistakes which will harm your program.
- Illegal read / Illegal write errors
- Use of uninitialised values
- Use of uninitialised or unaddressable values in
system calls - Illegal frees
- When a heap block is freed with an inappropriate
deallocation function - Overlapping source and destination blocks
- Memory leak detection
You can fine more help about valgrind use by downloading this pdf book.
Valgrind substitute for Windows:
if you are windows user then you can use following tool for same purpose.
You may like to read this also....
2 Comments to “How to detect memory leakage in C program?”
Post comment
Search in this website
our sponsors
latest comments
- sagar on List of all standard version of c language
- Mohit Dhukia on How to access/unblock songs.pk in india?
- shinto peter on How to configure mail from localhost ( wamp ) using PHP?
- tammylleanne on Implementation limitation of c programming language
- Deepak on How to access/unblock songs.pk in india?
Find us on Facebook
Top Authors
Find us on stackoverflow
Polls
My Bookmarks
- Audio/video Recorder & player application based on MATLAB
- check dependency of your binary
- defination of all standard c programming language function
- Great Question-Answer on c programming
- know what your c code means
- Limition of c programming language
- List of all version of c programming language
- Online c compiler
- php freelancing work
- some more stuff on C programming language
- Volatile Keyword in Embedded System
- Write Android application in c language
Nice article on valgrind.
Helps to a programmer a sure.
This is really something!