Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post


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;
delete 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.

Share your answer

Share as a guest

Please subscribe me to the CodeChef4U.

By creating your new answer, you agree to the privacy policy and terms of service.