471,336 Members | 1,263 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,336 software developers and data experts.

What is a dangling pointer, and how to avoid it?

what is dangling pointer, how it ishappen in program, what isthe solution
Aug 31 '07 #1
3 8028
dmjpro
2,476 2GB
what is dangling pointer, how it ishappen in program, what isthe solution
Right now I am working on J2EE means Java.
I totally Detached C or C++ or VC++ what I worked for 2 or 3 years.
But now a days I have look after those if I get time except my Project Works.
As far as I know, Dangling of pointer....When a pointer refers an Object which is already got destroyed.
And what is cause..It may be from our wrong Coding or some others.
I am not sure about the others.
And if the problems arised from you then the solution is in your Hand.
You need to take care of Object Construction and Destruction.

Kind regards,
Dmjpro.
Aug 31 '07 #2
Meetee
931 Expert Mod 512MB
what is dangling pointer, how it ishappen in program, what isthe solution
Dangling pointer is a pointer that points to an object that is already deleted. The following code illustrates this situation:

Expand|Select|Wrap|Line Numbers
  1. MyClass* p(new MyClass);
  2. MyClass* q = p;
  3. delete p;
  4. p->DoSomething();   //  p is now dangling!
  5. p = NULL;           // p is no longer dangling
  6. q->DoSomething();   // q is still dangling!
A popular technique to avoid dangling pointers is to use smart pointers. Please search google to know more about smart pointers.

Regards
Aug 31 '07 #3
JosAH
11,448 Expert 8TB
A popular technique to avoid dangling pointers is to use smart pointers. Please search google to know more about smart pointers.
There's also a ver nice article in the C/C++ Articles section about "handles"
which can do exactly what the OP wants.

kind regards,

Jos
Aug 31 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

13 posts views Thread by Aravind | last post: by
11 posts views Thread by John | last post: by
20 posts views Thread by __PPS__ | last post: by
1 post views Thread by sekhar_ps | last post: by
2 posts views Thread by ravi50 | last post: by
1 post views Thread by sridhard2406 | last post: by
20 posts views Thread by prashant.khade1623 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.