C++ Training: Valgrind

Valgrind is a tool for “memory debugging” of programs. It allows you to find places where memory is not properly allocated, which are difficult to find using traditional debuggers. Valgrind should be one of the first steps that you take when testing a program — even a simple one! It is available on Penn State’s clusters, or available on Linux.

Here are some “fun” programming errors you should avoid that Valgrind will help catch:

Trying to write outside the bound

double *a;
length = 5;
a = new double[length]; //a is now an array of size 5
for (int i = 0; i < 6; i++)
   a[i] = i; //error at i=5

The program will let you write a value at a[5], even though the only legal places to write are a[0] through a[4]. This is a corruption, since a[5] refers to a place where memory could be used by another variable. Valgrind will yell at you for this, and rightly so. Also be careful when allocating 2d and 3d arrays, since it is easy to confuse rows and columns, which will cause you major headaches.

Memory Leaks Inside Functions

void fun()
   double *a;
   a = new double[5];

Although the variable a gets destroyed as you leave the function, the memory that you allocated here doesn’t get destroyed at all! This is dangerous, especially if you call fun() many times.

Conditional Jump Depends on Uninitialized Variables

Even if you allocate memory properly, you may unfortunately perform this horrible error:

int y; //uninitialized at first
int x = 6;
int *a;
a = new int[5];

if (x < 5)
   y = 2;

x[y] = 7; //error, since y is not initialized yet
delete a; //a is deallocated properly

Here we managed memory correctly, but we accessed y before it had a value assigned to it. The danger here is that y really has some garbage value (i.e., -58342671), so the program may “sometimes” call the line correctly, and sometimes it won’t. Valgrind to the rescue!

This tutorial explains how to start using it.

5 thoughts on “C++ Training: Valgrind

  1. Pingback: C++ Training: Exercise 1 « Pat Reed Group Research Tips Blog

  2. While useful in determining memory leaks, Valgrind can be far more than a simple memory checker. Valgrind comes with a number extra tools, primarily Massif and Callgrind. Callgrind is useful in profiling a program, particularly if combined with the KCachegrind program to visualize the results. Massif is a heap profiler: it tracks memory usage and produces a graph showing heap usage over time, including information about which parts of the program are responsible for the most memory allocations. Both are very useful when trying to optimize code.

    An overview of the tools is here:

    And the documentation for using them is here:

  3. We had a similar error to the ‘conditional jump on uninitialized variable’, where there was a variable that was used to size a dynamically allocated array… but that variable was not actually initialized with a value before the call to the memory allocation. A nasty error that didn’t appear later… and to make matters worse it was in a Visual Studio project where there wasn’t access to valgrind or similar tools.

    Some great instructions are found here:

    including, about halfway down the page, a simple check_heap function that can tell you when the program’s heap got corrupted. So instead of having to wait for some ‘stochastic’ memory corruption error to creep up in a while after your initial problem, you can put in a few calls to check_heap() and it will cause the program to fail AS SOON AS the memory is corrupted. Happy day!

  4. Pingback: Water Programming Blog Guide (Part I) – Water Programming: A Collaborative Research Blog

  5. Pingback: Making Valgrind Easy – Water Programming: A Collaborative Research Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s