How to run valgrind with basic c example?

andrew picture andrew · Mar 29, 2015 · Viewed 22k times · Source

Installation:

bzip2 -d valgrind-3.10.1.tar.bz2
tar -xf valgrind-3.10.1.tar 

then:

./configure
make
make install

or simplier

sudo apt-get install valgrind

How to run valgrind on that simple program example1.c

#include <stdlib.h>
int main()
{
    char *x = malloc(100); /* or, in C++, "char *x = new char[100] */
    return 0;
}

Run:

valgrind --tool=memcheck --leak-check=yes example1
valgrind: example1: command not found

Output from console:

valgrind: example1: command not found

Answer

swalog picture swalog · Mar 29, 2015

It looks good. You only need to add a ./ before your executable. Without it, valgrind fails to find it and reports 'command not found'.

valgrind --tool=memcheck --leak-check=yes ./example1
                                          ^