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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sreenadh494
    New Member
    • Aug 2007
    • 13

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

    what is dangling pointer, how it ishappen in program, what isthe solution
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by sreenadh494
    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.

    Comment

    • Meetee
      Recognized Expert Contributor
      • Dec 2006
      • 928

      #3
      Originally posted by sreenadh494
      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:

      Code:
      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!
      A popular technique to avoid dangling pointers is to use smart pointers. Please search google to know more about smart pointers.

      Regards

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by zodilla58
        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

        Comment

        Working...