Sometimes we code and get segmentation fault all thanks to the mismanagement of the memory done in your code.

We allocate memory using malloc() and forget to free it. this also causes the leakage of memory.

There are times when we unknowingly free a memory location twice.

When our code is large, have thousands of lines of code , it becomes difficult to find out the memory leakage. So the saviour is Valgrind a tool which checks the memory leakage, and null pointer references and many other memory issues.

Usage of Valgrind :

First, we need to compile the program with –g option, -g enables the use of extra debugging information.

gcc -o test -g test.c

After successful compilation, we can check the memory leakage using the memcheck option.

valgrind --tool=memcheck --leak-check=yes --show-reachable=yes

You will get Summary of errors in the end after the check for leakages as Leak Summary

Leak Summary will tell you more about Definitely lost memory, Possibly lost memory, Still Reachable memory and Suppressed memory.

You can learn more about the Valgrind errors here .

Advertisements

2 thoughts on “VALGRIND: Memory Saviour

    1. I agree, and recently i had an interaction with a person who has an experience of 25 years in the industry, according to him also the memory leaks are the most difficult to handle. these tools helps us to deal with them.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s