please explain me what do we mean by dangling reference and dangling pointers.
please explain me what do we mean by dangling reference and dangling pointers.
I'm sorry, are we talking about an error or what? Any code involved or is this one of those abstract C++ things?
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