Dealing with dynamic memory is a very demanding task in low level programming languages like C, where the flexibility of the language is build on top of the assumption that programmers do not make mistakes, but they (we) know what they are doing at all times. As Peter Van Der Linden masterfully writes in this Expert C Programming book: “It’s not a bug. It is a language feature”. (if you have not read the book, but it. Now!). In this post I will present one of my favourite programming tools: Valgrind, which helps programmers (among other things) to deal with memory leaks in their programs.
Valgrind, as described by their creators is “an instrumentation framework for building dynamic analysis tools. There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. You can also use Valgrind to build new tools”. In other words, it is a tool that makes is *WAY* easier to debug memory and detect memory leaks. This includes invalid accesses to unallocated memory (reads and writes), invalid memory free, or access to uninitialized variables. The reality is that Valgrind is the product of quality research in the field of programming, programming languages and systems in general, put together in an open source framework. As a good friend of mine would say: Pure awesomeness! Valgrind is licensed under GNU GPL and can be downloaded from the website (valgrind.org).
The user manual provided by the Valgrind developers is very complete and well documented (link). For a quick start download the framework and execute one of your programs with: <valgrind –leak-check=yes –track-origins=yes myprog arg1 arg2 … arg>. You would probably be surprised what you discover 🙂
I believe that Valgrind is extremely well documented and easy to use, and therefore writing a post in the form of a tutorial would be silly since it would not add anything to what is already out there. In this way, my intention with this post is to spread the word and save headaches to those programmers who either do not know Valgrind or underestimate its helpfulness.