Memory checker - used to determine whether C program has memory errors
Memcheck
- Valgrind - popular Linux framework, comprises suite of tools that can be used to check for memory errors and profiling programs
- Valgrind can be run directly on existing executables - compile with
-g
argument for compiler to include debug info for Valgrind
- Good idea to turn off compiler optimizatilns with -o0 - slowdown give smore accurate Valgrind output
- Valgrind has additional built-in support to understand debug info by gcc compiler
- After preamble listening details of memcheck, Valgrind produces program output and summarizes heap usage - summary of errors detected
- note all error by Valgrind is sent to the standard error stream
AddressSanitizer, asan
- Part of clang - fast memory checker
- Must create a custom executable with AddressSanitizer enabled
- AddressSanitizer injects instrumentation at compile time
- Note that slowdown is minimal for AddressSanitizer
- enable at compile time with
-fsanitize=address
argument
- Recommended to use first level optimization, -O1, also use
-fno-omit-frame-pointer
to get “nicer stack traces”
- e.g.
clang -O1 -fsanitize=address -fno-omit-frame-pointer -Wall -g test.c -o test
- Note that AddressSanitizer will only produce output if something went wrong during program execution, i.e. memory error is detected
- clang also has other sanitizers
- Most errors are fairly obvious
Uninitialized Memory
- If we forget to initialize a variable, we will get an error like “Conditional jump or move depends on uninitialized value(s)”