Skip to main content
Be Bitwise

Dangling Pointers

Addresses & Pointers · Memory & Systems track

3 min read

In this module

When Pointers Go Wrong

Pointers are powerful and they're also the source of some of the worst bugs in software. A pointer is a number, period. An address. The CPU has no idea whether the data at that address is still alive. Keeping pointers honest falls entirely on the programmer (or the language runtime).

Use-After-Free

A use-after-free bug occurs when a program frees heap memory but continues using a pointer to that memory:

c
int *p = malloc(sizeof(int));
*p = 42;
free(p); // memory is released
printf("%d", *p); // BUG: p still holds the old address

Once free(p) runs, the allocator might hand that same memory to a different part of the program. Reading through p now returns whatever the new owner has stored. Writing through p corrupts the new owner's data. That's the textbook dangling pointer: a pointer that still points somewhere, but that somewhere is no longer yours.

The heap isn't the only source. A pointer to a local variable dangles the moment its function returns, because the next function call reuses the same stack space. Predict what the two reads through saved print, then run it.

Try itResults are undefined behaviour and can vary with the compiler, but at -O0 the second read usually shows the other function's 99.
C (GCC 9.2.0)

Nothing crashes and nothing warns at runtime. The pointer quietly starts reporting another function's data, which is exactly what makes dangling pointers so hard to track down.

A close cousin is the double free: calling free(p) a second time on a pointer that already dangles. The allocator then treats one block as free twice and can later hand it to two different callers at once. Modern glibc catches the simplest version and aborts, but it's undefined behaviour all the same, and you'll see how attackers exploit it in Memory Safety & Debugging.

Key Concept

A dangling pointer points to memory that has been freed or is otherwise no longer valid. Accessing it causes undefined behaviour - the program may crash, return garbage data, or silently corrupt other data.

Null Pointers

A null pointer holds address 0 (NULL in C). It says, plainly, "this pointer points to nothing." Dereferencing it is always a bug, and on most systems you get a segmentation fault for your trouble. Null pointers earn their keep as sentinels — "no data here yet" — but forgetting to null-check before dereferencing is one of the most common bugs in C and C++ code, full stop.

Buffer Overflows

A buffer overflow happens when a program writes past the end of an allocated block of memory. Allocate a 10-element array, write to index 15, and you've just clobbered whatever happened to be sitting in the adjacent bytes. Buffer overflows are also a foundational class of security vulnerability. Attackers craft inputs that overflow a buffer, overwrite a function's stack-stored return address, and redirect execution into code of their choosing.

Why Memory Safety Matters

Dangling pointers, null dereferences, buffer overflows — these three have caused more crashes, corruption, and CVEs than is comfortable to think about. They exist because C and C++ hand programmers direct, unchecked access to memory.

Modern languages each take a different swing at the problem:

LanguageApproach
RustOwnership and borrowing rules checked at compile time
GoGarbage collection, no pointer arithmetic
Java/C#Garbage collection, array bounds checking
Python/JSNo direct memory access at all

Every one of these trades a little control or performance for safety. The hardware underneath still moves around raw addresses. The language just bolts on guardrails so you don't have to police every pointer by hand.