This document provides information on how to use Valgrind to check for memory errors in C programs. It demonstrates how Valgrind can detect boundary errors, memory leaks, and accessing uninitialized variables. For boundary checks, it shows the output for a buffer overflow error. For memory leaks, it displays the leak summary output. For uninitialized variables, it shows the warnings produced when printing an uninitialized variable. The document also notes that Valgrind does not do static array boundary checks.
2. BinaryExe LLC
https://exebinary.wordpress.com/
Boundary Check
$ gcc -Wall -pedantic -g hello.c
$ valgrind ./a.out
==18584== Invalid write of size 4
==18584== at 0x400604: main (hello.c:9)
==18584== Address 0x52044a8 is 0 bytes after a block of size 40 alloc'd
==18584== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-
linux.so)
==18584== by 0x4005D1: main (hello.c:6)
Va l g r i n d l o g
4. BinaryExe LLC
https://exebinary.wordpress.com/
==18840== HEAP SUMMARY:
==18840== in use at exit: 40 bytes in 1 blocks
==18840== total heap usage: 3 allocs, 2 frees, 1,104 bytes allocated
==18840==
==18840== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==18840== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==18840== by 0x4005D1: main (hello.c:6)
==18840==
==18840== LEAK SUMMARY:
==18840== definitely lost: 40 bytes in 1 blocks
==18840== indirectly lost: 0 bytes in 0 blocks
==18840== possibly lost: 0 bytes in 0 blocks
==18840== still reachable: 0 bytes in 0 blocks
==18840== suppressed: 0 bytes in 0 blocks
==18840==
==18840== For counts of detected and suppressed errors, rerun with: -v
==18840== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Va l g r i n d
l o g
6. BinaryExe LLC
https://exebinary.wordpress.com/
==19095== Conditional jump or move depends on uninitialised value(s)
==19095== at 0x4E87B83: vfprintf (vfprintf.c:1631)
==19095== by 0x4E8F898: printf (printf.c:33)
==19095== by 0x40066A: main (hello.c:10)
==19095==
==19095== Use of uninitialised value of size 8
==19095== at 0x4E8476B: _itoa_word (_itoa.c:179)
==19095== by 0x4E8812C: vfprintf (vfprintf.c:1631)
==19095== by 0x4E8F898: printf (printf.c:33)
==19095== by 0x40066A: main (hello.c:10)
==19095==
==19095== Conditional jump or move depends on uninitialised value(s)
==19095== at 0x4E84775: _itoa_word (_itoa.c:179)
==19095== by 0x4E8812C: vfprintf (vfprintf.c:1631)
==19095== by 0x4E8F898: printf (printf.c:33)
==19095== by 0x40066A: main (hello.c:10)
...
==19095== ERROR SUMMARY: 8 errors from 8 contexts (suppressed: 0 from 0)