C Dangling Pointers

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Dangling Pointers?

A dangling pointer points to an object whose lifetime has ended: freed heap memory, a returned local variable, or a block-scoped object. Using it is undefined behavior and can silently corrupt data. Prevent it by setting freed pointers to NULL and never returning addresses of locals.

Remember
free(p);  p = NULL;      /* after free, reset */

int *bad(void) { int x = 5; return &x; }   /* dangling! */
int *ok(void)  { static int x = 5; return &x; }

Cheat Sheet

Keep these facts handy while you work with Dangling Pointers in C.

TopicDetail
FreeSet the pointer to NULL
LocalsNever return their address
ReallocOld pointers become invalid
DetectAddressSanitizer / Valgrind

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: const and Pointers.

Example 1 — Safe free and a static fix

Reset the pointer after free, and return a static instead of a local.

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

static int *counter(void) {
    static int c = 0;
    c++;
    return &c;
}

int main(void) {
    int *p = malloc(sizeof *p);
    if (!p) return 1;
    *p = 7;
    printf("value = %d\n", *p);
    free(p);
    p = NULL;
    if (p == NULL) printf("pointer reset to NULL\n");

    printf("counter: %d\n", *counter());
    printf("counter: %d\n", *counter());
    return 0;
}

How It Works

  1. After free(p) the old address is invalid, so p is reset to NULL.
  2. A later if (p) check can now catch accidental use.
  3. A static local lives for the whole program, so returning its address is safe.

Edge Cases & Pitfalls

Check these before relying on Dangling Pointers in real programs.

alias

Other copies of the pointer

Setting one pointer to NULL does not fix other pointers to the same block. Track ownership clearly.

realloc

Pointers into reallocated memory

After realloc moves the block, saved pointers into it dangle. Recompute them from the new base.

Key Takeaways

  • After free: set to NULL.
  • Lifetime: object must outlive its pointers.
  • Tools: sanitizers find these bugs.

One line: A dangling pointer points to an object whose lifetime has ended: freed heap memory, a returned local variable, or a block-scoped object.

Frequently Asked Questions

A pointer that still holds the address of memory that has been freed or gone out of scope. Using it is undefined behavior.
Compile with -fsanitize=address or run under Valgrind. Both report use-after-free and use-after-return errors.
Continue with "C const and Pointers" next. Tell const pointer-to-const, const pointer, and both apart. The C Pointers hub lists every topic in order.

Next: const and Pointers

Tell const pointer-to-const, const pointer, and both apart.

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.

9 people found this page helpful