Memory Leak, Dangling Pointer, and Double Free in c, cpp

⌚Time: 2026-03-26 13:27:00

👨‍💻Author: Jack Ge

Answer from AI

These three terms—Memory Leak, Dangling Pointer, and Double Free—are classic memory management errors in languages like C and C++. They each represent a different way of mishandling dynamic memory, leading to undefined behavior, crashes, or resource exhaustion.

Here’s a detailed breakdown of each.


1. Memory Leak

A memory leak occurs when dynamically allocated memory is no longer accessible by the program but is not deallocated (freed). The memory remains allocated until the program terminates, causing the program’s memory footprint to grow unnecessarily.

How it happens

You allocate memory (e.g., with malloc or new) but lose all references (pointers) to it before calling free or delete.

Example (C)

void leak_example() {
    int *p = (int*)malloc(sizeof(int)); // allocate memory
    *p = 10;
    p = NULL; // lost the address of allocated memory; can't free it now
    // memory leak: the allocated int is now unreachable
}

Consequences

Detection & Prevention


2. Dangling Pointer

A dangling pointer is a pointer that points to memory that has already been freed (or is otherwise invalid). Accessing (reading/writing) or freeing a dangling pointer leads to undefined behavior.

How it happens

You free memory but continue using the pointer that held its address, without resetting it to NULL or another valid value.

Example (C)

void dangling_example() {
    int *p = (int*)malloc(sizeof(int));
    *p = 42;
    free(p);        // memory is freed
    // p is now a dangling pointer
    *p = 100;       // UNDEFINED BEHAVIOR: writing to freed memory
}

Consequences

Detection & Prevention


3. Double Free

A double free occurs when you call free (or delete) on the same pointer more than once, after the memory has already been deallocated. This corrupts the heap metadata and leads to undefined behavior.

How it happens

You free a pointer, then later—perhaps through another pointer or by accident—free it again.

Example (C)

void double_free_example() {
    int *p = (int*)malloc(sizeof(int));
    free(p);
    free(p);  // DOUBLE FREE: heap metadata corruption
}

Consequences

Detection & Prevention


Comparison Table

Issue Definition Cause Common Consequence Prevention
Memory Leak Allocated memory no longer reachable, not freed Losing pointer before free Memory exhaustion, slowdown Smart pointers, RAII, tools
Dangling Pointer Pointer points to freed/invalid memory Using pointer after free Undefined behavior, use-after-free Set to NULL, smart pointers, avoid raw pointers
Double Free Freeing the same memory twice Two calls to free/delete Heap corruption, crash Set to NULL after free, single ownership

In C++ with RAII and Smart Pointers

Modern C++ largely eliminates these problems through RAII (Resource Acquisition Is Initialization) and smart pointers:

Example (C++ safe version)

#include <memory>

void safe_example() {
    auto p = std::make_unique<int>(42);
    // no manual free; no leak, no double free
    // p goes out of scope, memory automatically released
}

Summary

Term Key Idea
Memory Leak Memory is allocated but never freed.
Dangling Pointer Pointer still holds address of freed memory.
Double Free Memory is freed more than once.

All three are serious bugs that can lead to crashes, memory corruption, or security vulnerabilities. Modern C++ practices (RAII, smart pointers, containers) greatly reduce the risk, and sanitizer tools (ASan, Valgrind) are essential for catching them when they occur.