Dangling pointer code sample
Dangling pointer is a case where you are pointing to a memory which has been freed using the free () function.
MyClass* p(new MyClass);
MyClass* q = p;
p->DoSomething(); // p is now dangling!
p = NULL; // p is no longer dangling
q->DoSomething(); // q is still dangling!
Avoiding a dangling pointer is more of a programmer's exercise and use experience than any other techniques.
A popular technique to avoid dangling pointers is to use smart pointers.
Smart pointer is a pointer-like type with some additional functionality, e.g. automatic memory de- allocation, reference counting etc.