Skip to content

Instantly share code, notes, and snippets.

@mcarletti
Created December 17, 2019 12:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcarletti/e5f1e38bffc83e85c3ffa8b1faf6543b to your computer and use it in GitHub Desktop.
Save mcarletti/e5f1e38bffc83e85c3ffa8b1faf6543b to your computer and use it in GitHub Desktop.

How to Valgrind

Install

sudo apt update
sudo apt install valgrind

Write code

hello_valgrind.c

#include <stdlib.h>

void f(void)
{
    int* x = malloc(10 * sizeof(int));
    x[10] = 0;                          // problem 1: heap block overrun
}                                       // problem 2: memory leak -- x not freed

int main(void)
{
    f();
    return 0;
}

Compile

gcc hello_valgrind.c -o hello_valgrind -O0 -Wall -g -ggdb -fno-inline

Run Valgrind

valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --read-inline-info=yes --log-file="valgrind_log.txt" ./hello_valgrind
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment