How to Run Valgrind with Basic C Example?

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

2 Answers

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
                                          ^

First, compile your C program (-g is extremely important; without debug info in the executable valgrind cannot tell you line numbers from the source code where the violations occur nor the original line of the allocations of the memory being violated.):

gcc -g example1.c -o example1

Then run valgrind on the executable:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest