Search notes:

Valgrind

Building valgrind

valgrind is built using the standard ./configure, make, make install trilogoy.

Building targetted program

Options with which the targetted program should (or might) be built include:

Tools

When executing valgrind, the --tool option allows to select a tool (memcheck being the default).
Tools include:
memcheck Intercepts calls to malloc, new, free and delete, and generally checks all reads and writes of memory.
cachegrind A cache and prediction profiler. It simulates the CPU's I1, D1 and L2 caches and is used to make programs faster. (It seems that the simulation of the cache hierarchy is turned off by default and apparantly, perf that accesses hardeware counters is the better tool for such things)
callgrind An exension to cachegrind (call-graph generation). The collected data can be visualized with KCachegrind.
massif A heap profiler helping to reduce memory usage.
helgrind A thread debugger.
drd Detect errors in POSIX-multithreaded C/C++ programs, similar to helgrind.
dhat Examine how programs use heap allocations
lackey and nulgrind Used for testing and demonstration purposes.
bbv A bbv is a basic block vector and refers to a linear section of code with an entry point and an exit point. BBV is an experimental (SimPoint basic block vector generator) tool (also referred to as exp-bbv).
SGCheck Find overruns of arrays.

Simple example

prog.c

#include <stdlib.h>

int main() {

    char *mem_1 = malloc(100);
    char *mem_2 = malloc(200);

    free(mem_1); // Note: mem_1 is freed twice, but
    free(mem_1); // mem_2 is not freed at all.

}
Github repository about-Valgrind, path: /first/prog.c

Makefile

valgrind.out: prog
	valgrind --log-file=valgrind.out prog 

prog: prog.c
	gcc -g prog.c -o prog
Github repository about-Valgrind, path: /first/Makefile

See also

Memory alloction with libc
Memory leaks

Index