100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Dangling Pointers in C++

Understand how a pointer becomes 'dangling' after its target is freed or goes out of scope, and the safe practices that prevent it.

PointersIntermediate7 min readJul 7, 2026
Analogies

1. Intro

A dangling pointer is a pointer that still holds the address of memory that is no longer valid — because the memory has been freed (deleted) or because the variable it pointed to has gone out of scope. The pointer itself still "looks" valid (it holds some address), but using it to read or write data is undefined behavior, since that memory may have been reused for something else entirely.

🏏

Cricket analogy: A dangling pointer is like a fan still holding an entry pass to a stadium seat that's since been demolished and rebuilt as a parking lot — the pass looks valid, but using it now leads to unpredictable trouble.

2. Syntax

cpp
int* p = new int(10);
delete p;      // memory freed, but p still holds the old address
// p is now a dangling pointer

*p = 5;         // undefined behavior - accessing freed memory
p = nullptr;    // safe: neutralize the dangling pointer

3. Explanation

A pointer becomes dangling in a few common situations: (1) after calling delete on the memory it points to, without also resetting the pointer; (2) when it points to a local (stack) variable inside a function, and that function returns, destroying the variable while the pointer that referenced it still exists elsewhere; and (3) when multiple pointers point to the same heap block and one of them deletes it, leaving the others dangling. The danger is that a dangling pointer's address is not automatically invalidated or marked — the pointer still contains a real-looking address, and dereferencing it (*p) does not necessarily crash immediately. Instead it produces undefined behavior: it might silently read/write garbage data, corrupt unrelated memory that has since been reused, or crash unpredictably — bugs from dangling pointers are notoriously hard to track down because the failure can appear far away from the actual mistake. The standard defense is discipline: immediately set a pointer to nullptr after delete-ing it, never return the address of a local stack variable from a function, and avoid having multiple raw pointers independently own and free the same memory. Checking if (p != nullptr) before use is only effective if the pointer was properly nulled out after deletion — it does nothing to protect against a dangling pointer that was never reset.

🏏

Cricket analogy: A pointer dangles like a scorer still tracking 'Player 7' after that player retired and the jersey number was reissued to a rookie — the fix is to immediately retire the number (set to nullptr) after departure, never reference a substitute player who's left the ground, and avoid two scorers independently tracking and 'releasing' the same jersey number, since the resulting confusion may not show up until several overs later.

A dangling pointer points to memory that has already been freed or gone out of scope. Dereferencing it is undefined behavior — it may crash, corrupt unrelated data, or appear to work by accident. Always set a pointer to nullptr immediately after delete-ing it.

4. Example

cpp
#include <iostream>
using namespace std;

int main() {
    int* p = new int(10);
    cout << "Before delete, *p = " << *p << endl;

    delete p;        // memory is freed; p is now dangling
    p = nullptr;      // best practice: neutralize the dangling pointer

    if (p == nullptr) {
        cout << "p has been safely nulled out after delete." << endl;
    }

    // *p = 20;      // would be undefined behavior if p were not nulled

    return 0;
}

5. Output

text
Before delete, *p = 10
p has been safely nulled out after delete.

6. Key Takeaways

  • A dangling pointer still holds the address of memory that has been freed or gone out of scope.
  • Common causes: forgetting to reset a pointer after delete, returning the address of a local variable, or multiple pointers deleting the same memory.
  • Dereferencing a dangling pointer is undefined behavior — it may crash, corrupt data, or appear to work, making it hard to debug.
  • Best practice: set a pointer to nullptr immediately after delete, and never return addresses of local stack variables from functions.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#DanglingPointersInC#Dangling#Pointers#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep