Valgrind is a memory debugging tool available to Linux based machines. It runs your programs and keeps track of memory usage from calls such as malloc or free. If the program uses uninitialized memory or some chunk of memory is not freed, Valgrind will detect it and let you know. Easier debugging is just a few steps away.
First, we need to install Valgrind. The command for that is:
sudo apt-get install valgrind
To check whether there are memory leaks from a missing free call:
valgrind –leak-check=full -v ./[program name]
You should get a heap summary that will tell you how the number of memory allocations compare to the number of memory frees. If the two values are the same and you get the message, “All heap blocks were freed — no leaks are possible,” then congratulations! If not, it’s time to fix those memory leaks… (if you want to)
NOTE: There are 2 dashes in front of leak-check=full.