Oct
6

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.

 

JIGAR PATEL

hey I am an Artist who love to write code…! Well I am an EC graduate From Ganpat University and now i am working as Embedded software engineer in one private firm.. find me at here JigAr Patel

More PostsWebsiteTwitterFacebook

You may like to read this also....

2 Comments to “How to detect memory leakage in C program?”

  • Shah Dhaval October 4, 2012 at 9:28 am

    Nice article on valgrind.
    Helps to a programmer a sure.

  • Rhonda November 4, 2012 at 10:32 am

    This is really something!

Post comment

*

CommentLuv badge
Follow us on Twitter! Follow us on Twitter!

Search in this website

our sponsors

latest comments

Find us on Facebook

Top Authors

35 posts
saurabh
22 posts
10 posts

Find us on stackoverflow

Polls

Tell us who you are

View Results

Loading ... Loading ...

My Bookmarks

Sponsers Link