Finding a simple memory leak with Valgrind
Valgrind is a neat program for finding memory leaks and other memory-related bugs. It can even tell you if you put too much data inside an allocated memory area. These can all be hard bugs to find without a tool like Valgrind. Even if a program leaks memory or puts too much data in a memory area, it can still run fine for a long time. That's what makes those bugs so hard to find. But with Valgrind, we can check the program for all sorts of memory-related problems.
Getting started
For this recipe, you'll need the Valgrind tool installed on your computer. If you haven't already installed it, you can follow the instructions listed in the Technical requirements section of this chapter.
You'll also need the Make tool, the GCC compiler, and the Makefile from the Starting GDB recipe.
How to do it…
In this recipe, we'll write a program that allocates memory using calloc()
but never frees it with free...