Thread: dangling reference

  1. #1
    Unregistered
    Guest

    Question dangling reference

    please explain me what do we mean by dangling reference and dangling pointers.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    I'm sorry, are we talking about an error or what? Any code involved or is this one of those abstract C++ things?

  3. #3
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    His question is a honest one, Traveller.

    1.
    A dangling pointer is a pointer wich points to some random memory location without the knowledge of the coder... usually the after effect of not properly deleting it within the code or because of poor/inexistent pointer assigment error checking. It's called that way because the coder thinks the pointer is referring some memroy location while in fact it is not.

    It's most dangerous since it's hard to debug. As long as you have your pointers properly deleted as soon as you stop needing them and as long as you always check if a pointer was properly assigned, there should be no problems.

    Things get a little bit more complicated when creating classes with dynamic data members (data members that are pointers)... actually these can quickly become a nightmare if not properly implemented. There is a "little" more to these than just error checking and deleting.

    2.
    A dangling pointer can also be called that way when you declare a pointer but don't assign anything to it. You should always assign something to your pointers. Even if to NULL. As far as memory is concerned, not assigning a pointer is exactly the same thing as 1.

    Make it a rule to never have more than 1 statement in which to declare and assign your pointer. This may look a little exagerated, but discipline is the way to go when memory managment is concerned.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM