Using valgrind

  1. The valgrind utility can be used to detect memory problems (e.g., dynamically-allocated memory problems, accessing uninitialized variables).
  2. To use valgrind make sure you compile your code with the -g option.
  3. The command you should execute (assuming a.out is the executable) is:
    valgrind   --track-origins=yes   --leak-check=full   --show-leak-kinds=all   -s   a.out
  4. If you replace the -s option with -q, you will only see valgrind output if there are any errors.
    valgrind   --track-origins=yes   --leak-check=full   --show-leak-kinds=all   -q   a.out
  5. You should create an alias (similar to what you may have done with gcc) for the above command(s).

Examples

The following illustrates the information valgrind generates when we have not freed dynamically-allocated memory.

            ==2845066== 8 bytes in 1 blocks are definitely lost in loss record 1 of 2
            ==2845066==    at 0x484482F: malloc (vg_replace_malloc.c:431)
            ==2845066==    by 0x401269: create_photo (photo_album.c:16)
            ==2845066==    by 0x401502: add_photo_to_album (photo_album.c:80)
            ==2845066==    by 0x4011D4: main (public03.c:11)
            ==2845066==
            ==2845066== 12 bytes in 1 blocks are definitely lost in loss record 2 of 2
            ==2845066==    at 0x484482F: malloc (vg_replace_malloc.c:431)
            ==2845066==    by 0x401269: create_photo (photo_album.c:16)
            ==2845066==    by 0x401502: add_photo_to_album (photo_album.c:80)
            ==2845066==    by 0x4011BE: main (public03.c:10)
        

The following illustrates the information valgrind generates when we tried to access uninitialized memory allocated in the heap.

            ==2846814== Use of uninitialised value of size 8
            ==2846814==    at 0x48D156B: _itoa_word (in /usr/lib64/libc.so.6)
            ==2846814==    by 0x48EB11B: __vfprintf_internal (in /usr/lib64/libc.so.6)
            ==2846814==    by 0x48D74DE: printf (in /usr/lib64/libc.so.6)
            ==2846814==    by 0x401225: main (public03.c:17)
            ==2846814==  Uninitialised value was created by a heap allocation
            ==2846814==    at 0x484482F: malloc (vg_replace_malloc.c:431)
            ==2846814==    by 0x4011A6: main (public03.c:9)
        

The following illustrates the information valgrind generates when we tried to access uninitialized memory allocated in the stack.

            ==2846814== Use of uninitialised value of size 8
            ==2846814==    at 0x48D156B: _itoa_word (in /usr/lib64/libc.so.6)
            ==2846814==    by 0x48EB11B: __vfprintf_internal (in /usr/lib64/libc.so.6)
            ==2846814==    by 0x48D74DE: printf (in /usr/lib64/libc.so.6)
            ==2846814==    by 0x40120E: main (public03.c:16)
            ==2846814==  Uninitialised value was created by a stack allocation
            ==2846814==    at 0x401186: main (public03.c:6)            
        

Additional Information and Options You May Want to Try

Options for a Mac

In a Mac you could try the leaks command (e.g., leaks --atExit -- a.out). Additional information at https://computerscience.chemeketa.edu/guides/valgrind/leaks/.

Web Accessibility