0Pricing
C Academy · Lesson

Detecting Leaks

memcheck basics.

What A Leak Is

A memory leak happens when you allocate heap memory with malloc, calloc, or realloc and lose every pointer to it before calling free.

The block stays reserved but unreachable until the process exits. In long-running programs, leaks accumulate until memory runs out.

A Minimal Leak

This program leaks one block of 40 bytes.

The pointer p is a local variable. When main returns, p disappears but the heap block it pointed at is never freed.

#include <stdlib.h>

int main(void) {
    int *p = malloc(10 * sizeof(int));
    p[0] = 1;
    return 0; /* never free(p) */
}

All lessons in this course

  1. Why Valgrind
  2. Detecting Leaks
  3. Invalid Access
  4. Reading Reports
← Back to C Academy