Thread: deallocate pointer

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    8

    deallocate pointer

    My question is, if the following sentence is true or false? why?

    “It is important to deallocate a pointer to avoid dangling pointers.”

    Thanks

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Would you describe in your words how you understand "dangling pointer"?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    My understanding of dangling pointer is,'when the memory to which pointer is pointing to, is deallocated then the pointer becomes dangling. Pointer also becomes dangling when you try to dereference uninitialized pointer.So programmer has to be very careful not to dereference dangling pointer otherwise unnecessary results occur.To avoid this one should set the pointer to NULL after deallocation of the memory it is pointing to.

    'you never deallocate pointer but you deallocate memory it is pointing to' . So in my original question I have asked, the sentence used is 'deallocate a pointer' which is confusing me. It is the question in my assignment I have to write,the correct version of it, if it is wrong.

    waiting for the reply
    Thanks.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is not what I would say a "dangling pointer is".

    Look at this:
    http://en.wikipedia.org/wiki/Dangling_pointer

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    So 'deallocating a pointer' means setting pointer's value to NULL?

    Thanks

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sangit View Post
    So 'deallocating a pointer' means setting pointer's value to NULL?

    Thanks
    No, deallocating is calling free or delete. Setting the pointer to NULL is a preacaution to prevent the variable from being used accidentally AFTER it has been freed. [which is one form of dangling pointer - you have a pointer to memory no longer owned by the application].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    Thank you so much

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    sorry, still confused little bit..
    Now my understading is deallocating a pointer does not avoid dangling pointer because pointer value is not changed yet. after deallocation setting the pointer value to NULL only avoid dangling pointer..

    Please correct me if I am wrong.
    Thanks.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    That is not what I would say a "dangling pointer is".

    Look at this:
    http://en.wikipedia.org/wiki/Dangling_pointer

    --
    Mats
    Quote Originally Posted by Wikipedia
    Dangling pointers and wild pointers in computer programming are pointers that do not point to a valid object of the appropriate type. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory.
    Quote Originally Posted by sangit
    when the memory to which pointer is pointing to, is deallocated then the pointer becomes dangling
    These sound like the same thing to me, except the Wikipedia explanation is a bit more detailed by saying "without modifying the value of the pointer"...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sangit View Post
    sorry, still confused little bit..
    Now my understading is deallocating a pointer does not avoid dangling pointer because pointer value is not changed yet. after deallocation setting the pointer value to NULL only avoid dangling pointer..

    Please correct me if I am wrong.
    Thanks.
    Correct, if you do not change the value of the pointer, then you do indeed have a dangling pointer - a pointer to memory that you do not own. Setting it to NULL does not deallocate the memory, it sets the pointer to a NULL value - which is the way to indicate that "this pointer is no longer pointing to valid memory, it is NULL and has no content".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Deallocating objects or "pointers" has to do with avoiding memory leaks. Naturally, since the object is not valid after it is deallocated, deallocation is a way to obtain, not fight dangling pointers.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by anon View Post
    Deallocating objects or "pointers" has to do with avoiding memory leaks. Naturally, since the object is not valid after it is deallocated, deallocation is a way to obtain, not fight dangling pointers.
    Yes, and dangling pointers in this sense isn't a bad thing. Setting the pointer to NULL makes it "no longer dangling", and makes it easier to detect use of a pointer that has been freed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    Quote Originally Posted by sangit View Post
    ...So programmer has to be very careful not to dereference dangling pointer otherwise unnecessary results occur...
    I think you probably mean undefined results but either way 'undefined results' usually means your program will blow up on you with a segfault (windows = "access violation") and a crash.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    'undefined results' usually means your program will blow up on you with a segfault (windows = "access violation") and a crash.
    Dangling pointers have the annoying tendency to not crash your program and instead silently corrupt data. That's why you set them to NULL, because dereferencing that will crash your program.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    Good point. Ultimately the program will probably crash but it could be well after the data a pointer was pointing to was freed. Those are the most annoying bugs to find if you're not familiar with a good debugger.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM