C-Valgrind

Step 1 : Install valgrind - Dynamic analyser

$ sudo apt-get install valgrind
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  g++-7 gcc-8-base:i386 gir1.2-mutter-2 libargon2-0 libbind9-160 libbison-dev libboost-date-time1.65.1 libboost-filesystem1.65.1 libboost-iostreams1.65.1
  libboost-locale1.65.1 libboost-system1.65.1 libboost-thread1.65.1 libcapnp-0.6.1 libcdio17 libclang1-6.0 libcomerr2:i386 libcommons-dbcp-java libcommons-pool-java
  libcwidget3v5 libdns-export1100 libdns1100 libdouble-conversion1 libecj-java libevent-2.1-6 libffi6:i386 libgdbm5 libgl1-mesa-glx:i386 libgmime-3.0-0
  libgnome-desktop-3-17 libgspell-1-1 libgweather-3-15 libhogweed4:i386 libhunspell-1.6-0 libicu60:i386 libip4tc0 libip6tc0 libiptc0 libirs-export160 libirs160
  libisc-export169 libisc169 libisccc160 libisccfg-export160 libisccfg160 libisl19 libjs-sphinxdoc libjs-underscore libjson-c3 liblinear3 libllvm10:i386 libllvm11
  libllvm11:i386 libllvm6.0 liblouis14 liblouisutdml8 liblwres160 libmagickcore-6.q16-3 libmagickcore-6.q16-3-extra libmagickwand-6.q16-3 libmozjs-52-0
  libmutter-2-0 libncurses5:i386 libnettle6:i386 libnss-myhostname libntfs-3g88 liborcus-0.13-0 libperl5.26 libpoppler73 libpotrace0 libprocps6 libprotobuf-lite10
  libprotobuf10 libprotoc10 libpython-all-dev libpython3.6 libpython3.6-dev libpython3.6-minimal libpython3.6-stdlib libqgsttools-p1 libqpdf21 libraw16 libreadline7
  libruby2.5 libsane1 libsane1:i386 libsndio6.1:i386 libspeexdsp1:i386 libstdc++-7-dev libsynctex1 libtexlua52 libtinfo5:i386 libtomcat8-java libvpx5 libwireshark11
  libwiretap8 libwscodecs2 libwsutil9 nplan python-all python-all-dev python-asn1crypto python-bzrlib python-cffi-backend python-configobj python-configparser
  python-crypto python-cryptography python-entrypoints python-enum34 python-httplib2 python-idna python-ipaddress python-keyring python-launchpadlib
  python-lazr.restfulclient python-lazr.uri python-oauth python-pycurl python-secretstorage python-simplejson python-wadllib python-wheel python-xdg
  python-zope.interface python3-asn1crypto python3-oauth python3-zope.interface python3.6 python3.6-dev python3.6-minimal ruby-did-you-mean ruby2.5 shim
  wireshark-gtk
Use 'sudo apt autoremove' to remove them.
Suggested packages:
  valgrind-dbg valgrind-mpi kcachegrind alleyoop valkyrie
The following NEW packages will be installed:
  valgrind
0 upgraded, 1 newly installed, 0 to remove and 26 not upgraded.
Need to get 20.3 MB of archives.
After this operation, 90.0 MB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 valgrind amd64 1:3.15.0-1ubuntu9.1 [20.3 MB]
Fetched 20.3 MB in 2s (10.9 MB/s)    
Selecting previously unselected package valgrind.
(Reading database ... 424171 files and directories currently installed.)
Preparing to unpack .../valgrind_1%3a3.15.0-1ubuntu9.1_amd64.deb ...
Unpacking valgrind (1:3.15.0-1ubuntu9.1) ...
Setting up valgrind (1:3.15.0-1ubuntu9.1) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for doc-base (0.10.9) ...
Processing 1 added doc-base file...
Registering documents with scrollkeeper...

Step 2 : Create a file test.c with memleak

#include <stdio.h>
#include <stdlib.h>

int main()
{
  char *p;

  // Allocation #1 of 30 bytes
  p = (char *) malloc(30);

  // Allocation #2 of 12 bytes
  p = (char *) malloc(8);
  free(p);

  // Allocation #3 of 16 bytes
  p = (char *) malloc(25);

  return 0;
}

Step 3 : Compile test.c

$ gcc -o test -g test.c

Step 4 : Run program with valgrind

$ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./test -s
==66488== Memcheck, a memory error detector
==66488== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==66488== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==66488== Command: ./test -s
==66488== 
==66488== 
==66488== FILE DESCRIPTORS: 3 open at exit.
==66488== Open file descriptor 2: /dev/pts/6
==66488==    <inherited from parent>
==66488== 
==66488== Open file descriptor 1: /dev/pts/6
==66488==    <inherited from parent>
==66488== 
==66488== Open file descriptor 0: /dev/pts/6
==66488==    <inherited from parent>
==66488== 
==66488== 
==66488== HEAP SUMMARY:
==66488==     in use at exit: 55 bytes in 2 blocks
==66488==   total heap usage: 3 allocs, 1 frees, 63 bytes allocated
==66488== 
==66488== 25 bytes in 1 blocks are definitely lost in loss record 1 of 2
==66488==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==66488==    by 0x1091A6: main (in /home/test/Desktop/training/valgrind/test)
==66488== 
==66488== 30 bytes in 1 blocks are definitely lost in loss record 2 of 2
==66488==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==66488==    by 0x10917E: main (in /home/test/Desktop/training/valgrind/test)
==66488== 
==66488== LEAK SUMMARY:
==66488==    definitely lost: 55 bytes in 2 blocks
==66488==    indirectly lost: 0 bytes in 0 blocks
==66488==      possibly lost: 0 bytes in 0 blocks
==66488==    still reachable: 0 bytes in 0 blocks
==66488==         suppressed: 0 bytes in 0 blocks
==66488== 
==66488== For lists of detected and suppressed errors, rerun with: -s
==66488== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

Step 5 : Fix the memleaks in file test.c and compile again

#include <stdio.h>
#include <stdlib.h>

int main()
{
  char *p;

  // Allocation #1 of 30 bytes
  p = (char *) malloc(30);
  free(p);

  // Allocation #2 of 12 bytes
  p = (char *) malloc(8);
  free(p);

  // Allocation #3 of 16 bytes
  p = (char *) malloc(25);
  free(p);

  return 0;
}

Step 6 : Run program with valgrind

$ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./test -s
==66595== Memcheck, a memory error detector
==66595== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==66595== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==66595== Command: ./test -s
==66595== 
==66595== 
==66595== FILE DESCRIPTORS: 3 open at exit.
==66595== Open file descriptor 2: /dev/pts/6
==66595==    <inherited from parent>
==66595== 
==66595== Open file descriptor 1: /dev/pts/6
==66595==    <inherited from parent>
==66595== 
==66595== Open file descriptor 0: /dev/pts/6
==66595==    <inherited from parent>
==66595== 
==66595== 
==66595== HEAP SUMMARY:
==66595==     in use at exit: 0 bytes in 0 blocks
==66595==   total heap usage: 3 allocs, 3 frees, 63 bytes allocated
==66595== 
==66595== All heap blocks were freed -- no leaks are possible
==66595== 
==66595== For lists of detected and suppressed errors, rerun with: -s
==66595== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)