Thread: Preventing Dangling Pointers

  1. #1
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137

    Preventing Dangling Pointers

    What are your recommendations as far as preventing the "Dangling pointer problem?" Do you ALWAYS assign NULL to the pointer as soon as you free it? Are there any other methods you recommend or have tried? Thanks.

    Btw: Dangling pointers in this case refers to pointers which point to memory that has already been freed and thus a deference would be bad

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What are your recommendations as far as preventing the "Dangling pointer problem?"
    Avoid using dynamic memory except when absolutely necessary.

    Do you ALWAYS assign NULL to the pointer as soon as you free it?
    No. Dereferencing a NULL pointer is still a problem, so it's best that you just remember not to play with a pointer that has been freed.

    Jim

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Of course, if there's any chance to use the pointer in the future, you should make it NULL. If not though, it's pointless in my opinion.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About Dangling Pointer
    By Cleaner in forum C++ Programming
    Replies: 5
    Last Post: 06-13-2012, 12:44 AM
  2. Question about Stray or Dangling Pointers
    By Jonathan Yaniv in forum C++ Programming
    Replies: 13
    Last Post: 06-12-2012, 11:27 PM
  3. dangling pointers
    By lydiapeter in forum C Programming
    Replies: 5
    Last Post: 08-31-2005, 03:49 AM
  4. Dangling Else error?
    By OttoDestruct in forum C++ Programming
    Replies: 9
    Last Post: 09-01-2004, 06:25 PM
  5. dangling reference
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-04-2002, 04:01 AM

Tags for this Thread