C Memory Leaks

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Memory Leaks?

A memory leak happens when heap memory is allocated but never freed and all pointers to it are lost. Leaks accumulate in long-running programs. Typical causes are early returns, overwriting a pointer, and forgetting free. Tools such as Valgrind and AddressSanitizer report leaks.

Remember
char *p = malloc(100);
p = malloc(200);    /* first block leaked! */

gcc -fsanitize=address -g file.c    /* detect leaks */

Cheat Sheet

Keep these facts handy while you work with Memory Leaks in C.

TopicDetail
CauseLost pointer or missing free
SymptomGrowing memory use
DetectValgrind, ASan, LeakSanitizer
FixSingle owner, cleanup path

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Safe Memory Practices.

Example 1 — Counting live allocations with wrappers

A tiny counter makes leaks visible without external tools.

C
#include <stdio.h>
#include <stdlib.h>

static int live = 0;

static void *xmalloc(size_t n) { live++; return malloc(n); }
static void xfree(void *p)     { if (p) live--; free(p); }

int main(void) {
    int *a = xmalloc(sizeof *a);
    int *b = xmalloc(sizeof *b);
    printf("live allocations: %d\n", live);

    xfree(a);
    printf("after freeing a: %d\n", live);

    xfree(b);
    printf("after freeing b: %d\n", live);
    return 0;
}

How It Works

  1. Each wrapped allocation increments live; each free decrements it.
  2. Anything above 0 at exit is a leak.
  3. Real projects use sanitizers instead of hand-written counters.

Edge Cases & Pitfalls

Check these before relying on Memory Leaks in real programs.

early

Early returns

A return between malloc and free skips cleanup. Use a single exit label with goto cleanup.

over

Overwriting the only pointer

Assigning a new value to the only pointer to a block orphans the block.

Key Takeaways

  • Pair: allocate and free in the same logic.
  • Tools: ASan and Valgrind.
  • Cleanup path: one exit point.

One line: A memory leak happens when heap memory is allocated but never freed and all pointers to it are lost.

Frequently Asked Questions

A leak occurs when dynamically allocated memory is no longer reachable but has not been freed, so it cannot be reused until the process exits.
Run the program under Valgrind (valgrind --leak-check=full ./a.out) or compile with -fsanitize=address.
Continue with "C Safe Memory Practices" next. A checklist for allocating, using, and releasing memory safely. The C Dynamic Memory Allocation hub lists every topic in order.

Next: Safe Memory Practices

A checklist for allocating, using, and releasing memory safely.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful