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.