What is Valgrind? 
Valgrind (downloadable here) is a utility for debugging programs for the x86 and x86-64 Linux platforms. It has recently become highly popular as it can be used to easily track down memory management and threading bugs that are hard to track down otherwise.
How to install it 
There is a "generic" option, available on most systems, which involves compiling:
user> wget http://valgrind.org/downloads/valgrind-3.8.1.tar.bz2 user> bunzip2 valgrind-3.8.1.tar.bz2 user> tar -xvf valgrind-3.8.1.tar user> cd /valgrind-3.8.1 user> ./configure user> make user> su root root> make install
On most Linux distributions, however, you can use the package management system. For example, in Debian GNU/Linux (and derivatives) simply run:
apt-get install valgrind
How to use it 
Valgrind can be run simply by prefixing the command line that you run with valgrind
./myprogram -o option
valgrind ./myprogram -o option
This simple test will check that the memory accesses with in the program are correct. Do not be surprised when you get messages about code that you know is not a problem. This program tests every access and some programs are forgiving about existing errors. It is always worth fixing these extra errors because they will be impossible to track down when they do cause a breakage in code.
You may prefer to run valgrind and log to a text file, with the following options:
valgrind --leak-check=full --freelist-vol=100000000 --log-file-exactly=log.txt -v ./myprogram
How it works 
Valgrind is essentially an x86 machine-code interpreter. In fact it runs as a just-in-time compiler converting the machine code to an internal language, instrumenting that language and and then code-generating from that language. Valgrind instruments the code to monitor memory allocation, deallocation, writes and reads, which lets it hold a bit-map of memory state. hence it can report attempts to read data from memory that has never yet been written, or using recently-freed memory. Under Valgrind a program will take something like 2-10 times longer than when run uninstrumented.
Callgrind is a related program that uses the same x86 interpreter technology to instrument the code to log routine calls and generate a file that can be analyzed to show time spent in various routines and the call paths involved.