01 / 21
?

Pointer
Pitfalls

Master the most dangerous bugs in C++: dangling pointers, uninitialized pointers, and memory mismanagement. Learn to spot, prevent, and fix them.

C++

What Is a Pointer?

A pointer is a variable that stores the memory address of another variable. Instead of holding data directly, it holds a reference to where that data lives in memory.

ptr 0x7ffc... holds an address x 42 actual data

Why Pointers Go Wrong

Pointers are powerful but unforgiving. C++ gives you direct access to memory, but the language does not automatically verify that a pointer is valid before you use it. This creates several categories of bugs:

Uninitialized
Pointer declared but never assigned a valid address. It holds garbage data.
Dangling
Pointer once valid, but the memory it references has been freed or gone out of scope.
Null Deref
Pointer is explicitly set to nullptr but the program tries to read or write through it.

The Pointer Lifecycle

Declare int* ptr; Initialize ptr = &x; Use *ptr = 10; Invalidate delete / scope Danger Zone: Using ptr after invalidation The pointer still exists, but the memory it references is gone. Safe: Always set ptr = nullptr after delete

Uninitialized Pointers

When you declare a pointer without assigning it a value, it contains whatever garbage data happened to be in that memory location. Dereferencing it is undefined behavior.

int* ptr; // not initialized: holds garbage address cout << *ptr; // UNDEFINED BEHAVIOR!
ptr 0xBAADF00D garbage address ??? unknown memory Could crash, corrupt data, or silently succeed

Dangling Pointers

A dangling pointer points to memory that has been freed or has gone out of scope. The address is still stored, but the data there is no longer yours.

int* ptr = new int(42); delete ptr; // memory freed cout << *ptr; // DANGLING: undefined behavior!
ptr 0x55a0... still holds address FREED memory returned to OS The address is valid syntax but invalid memory

Three Causes of Dangling

1
Delete without nulling: After delete ptr, the pointer still holds the old address. Any dereference is undefined behavior.
2
Returning address of local: A function returns &localVar. Once the function ends, that stack frame is reclaimed.
3
Scope exit: A pointer references a variable in an inner block. When that block ends, the variable is destroyed but the pointer lives on.

Uninitialized vs. Dangling

Uninitialized Dangling
When At declaration After delete or scope exit
Address held Random garbage Previously valid address
Was it ever valid? Never Once, but no longer
Detection Compiler warnings Hard: may work sometimes
Fix Always initialize Set to nullptr after delete

The Golden Rules

1
Always initialize: Set every pointer to nullptr or a valid address at declaration.
2
Null after delete: Immediately write ptr = nullptr; after every delete ptr;.
3
Check before deref: Guard every dereference with if (ptr != nullptr).
4
Never return local addresses: Do not return &localVar from a function.
5
Prefer smart pointers: Use std::unique_ptr or std::shared_ptr to automate lifetime management.

Pointer Safety Checklist

Before using any pointer, walk through this checklist:

1
Declared? Is the pointer variable declared with the correct type?
2
Initialized? Does it point to a valid object or nullptr?
3
Alive? Is the object it references still within its lifetime?
4
Null-checked? Did you verify it is not nullptr before dereferencing?
5
Cleaned up? After delete, is it set to nullptr?

Example 1: Uninitialized Pointer

// BUG: uninitialized pointer int* scores; scores[0] = 95; // writing to garbage address!
// FIX: initialize with allocation or nullptr int* scores = new int[30]; // allocated scores[0] = 95; // safe // ... use scores ... delete[] scores; scores = nullptr; // nulled

Example 2: Dangling After Delete

int* val = new int(7); cout << *val; // prints 7, fine delete val; cout << *val; // DANGLING: undefined behavior
Before delete val 0x55a0 7 After delete val 0x55a0 ? Fix: val = nullptr; after delete

Example 3: Returning a Local Address

int* getNumber() { int x = 42; // x lives on the stack return &x; // returning address of local! } int* p = getNumber(); cout << *p; // DANGLING: x is destroyed
// FIX: allocate on the heap, or return by value int* getNumber() { int* x = new int(42); // heap allocation return x; // caller must delete }

Null Pointer Dereference

Setting a pointer to nullptr is a safety net, not a fix by itself. You must check before you dereference.

int* ptr = nullptr; cout << *ptr; // CRASH: null dereference // Safe pattern: if (ptr != nullptr) { cout << *ptr; // only runs if valid } else { cout << "Pointer is null"; }

Common Mistakes

✕ Double Delete
Calling delete twice on the same pointer without setting it to nullptr in between. Causes heap corruption.
✕ Shallow Copy
Copying a pointer copies the address, not the data. Both pointers now share the same memory. Delete one, the other dangles.
✕ Array Mismatch
Using delete instead of delete[] on array allocations (or vice versa). Undefined behavior.

Pitfall Summary

Pitfall Symptom Prevention
Uninitialized Random crash or corruption Initialize to nullptr or valid address
Dangling Intermittent bugs, data corruption Set to nullptr after delete
Null deref Immediate crash (segfault) Check != nullptr before use
Double delete Heap corruption Null after delete, use smart pointers
Returning local addr Dangling after function returns Use heap allocation or return by value

Question 1 of 5

What is an uninitialized pointer?

Question 2 of 5

What type of pointer error does this code demonstrate?

Question 3 of 5

Which line should be added immediately after delete ptr; to prevent a dangling pointer?

Question 4 of 5

Why is the final line of this code undefined behavior?

Question 5 of 5

Which modern C++ feature best prevents dangling pointers and memory leaks by automatically managing object lifetimes?