Like Tree1Likes
  • 1 Post By antred

About Dangling Pointer

This is a discussion on About Dangling Pointer within the C++ Programming forums, part of the General Programming Boards category; Hi, I am new to this thread and programming and wanted to know about "Dangling Pointer". Does dangling pointer only ...

  1. #1
    Banned
    Join Date
    Jun 2012
    Posts
    2

    About Dangling Pointer

    Hi,

    I am new to this thread and programming and wanted to know about "Dangling Pointer".


    Does dangling pointer only arises when 2 objects point at the same memory location or it can even arise if 1 object points at the same memory location?

    Thanks

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Let me google that for you

    Both, and many more ways.
    Last edited by manasij7479; 06-12-2012 at 02:42 PM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    227
    A dangling pointer is nothing more than a pointer that points to a memory address that used to be where something valid (an instance of a class, an integer, the first element of an array ... whatever) was located, but that "something valid" is no longer there - perhaps because it has been deleted in the meantime.

    There are any number of ways to accidentally create a dangling pointer.

  4. #4
    Banned
    Join Date
    Jun 2012
    Posts
    2
    Hi antred,

    Thanks for this answer.

    So dangling pointer can happen if 1 or more than 1 objects point at the same memory location?

  5. #5
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    227
    Quote Originally Posted by Cleaner View Post
    So dangling pointer can happen if 1 or more than 1 objects point at the same memory location?
    I'm not sure if you're thinking the right thing, but you worded it wrong. Objects don't "point" at memory locations. Objects occupy memory locations. Only one object can live at any memory location at any one time. You can, however, have multiple pointers point to the same memory location, but that in itself doesn't create dangling pointers.

    Simplest example for a dangling pointer:

    Code:
    int* ptrToAnInt = new int; // here we create an integer and assign its address to our pointer
    
    delete ptrToAnInt; // <-- from this point on, the integer that used to live at the location pointed to by ptrToAnInt is no longer there
    
    *ptrToAnInt = 5; // <-- UNDEFINED BEHAVIOR! There is no longer an integer at the location pointed to by ptrToAnInt.
    P.S. To avoid dangling pointers, you want to set pointers to null the moment the thing they point to stops existing:


    Code:
    int* ptrToAnInt = new int;
    
    delete ptrToAnInt;
    ptrToAnInt = 0; // <-- IMPORTANT! Set pointer to null to make it unmistakably clear that it no longer points to anything.
    
    if ( ptrToAnInt ) // <-- Then check pointer for null before trying to dereference it.
    {
        *ptrToAnInt = 5;
    }
    Last edited by antred; 06-12-2012 at 03:19 PM.
    Salem likes this.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    134
    In C++ "Shallow copy" leads dangling pointer. It is another common mistake done by beginners.
    S_ccess is waiting for u. Go Ahead, put u there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer dangling or not
    By vaibhav in forum C++ Programming
    Replies: 16
    Last Post: 08-05-2006, 06:39 PM
  2. Dangling pointer
    By sunnypalsingh in forum C Programming
    Replies: 5
    Last Post: 10-20-2005, 02:28 PM
  3. dangling pointers
    By lydiapeter in forum C Programming
    Replies: 5
    Last Post: 08-31-2005, 03:49 AM
  4. Dangling pointer
    By Extrovert in forum Tech Board
    Replies: 5
    Last Post: 03-13-2003, 12:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21