0

What is Dangling Pointer?

Can anyone please explain what Dangling Pointer is? How can that be avoided ? Any example is possible.

17th Nov 2016, 5:02 AM
Pranjal Upadhyay
Pranjal Upadhyay - avatar
6 Réponses
+ 2
Dangling pointers are variables which stores memory addresses that points to garbage after being deleted. example:- int var; int * pVar = new int[]; pVar = &var; delete[] pVar; pVar should be assigned to a variable address after being deleted. normally to NULL. pVar = NULL; - Not a dangling pointer. if you dont quickly assign pVar to any variable address, it becomes a dangling pointer. pVar; - dangling pointer.
17th Nov 2016, 6:04 PM
Franky BrainBox
Franky BrainBox - avatar
+ 1
Basically a dangling pointer occurs when you delete data that is being pointed to and don't adjust the pointer accordingly. So it will be pointing to some space in memory that no longer contains anything. For avoiding it, you just have to be careful. Pointers can be tricky at times.
17th Nov 2016, 5:16 AM
scott johnson
scott johnson - avatar
+ 1
Assign nullptr to pointers not pointing to anything, not NULL. NULL is outdated when it comes to pointers; mainly used in C.
19th Nov 2016, 3:29 AM
Cohen Creber
Cohen Creber - avatar
0
one more que based on ur ans.. u told if we delete data, is it data that is deleted or the pointer is deleted. ??
17th Nov 2016, 5:21 AM
Pranjal Upadhyay
Pranjal Upadhyay - avatar
0
the data that the pointer is pointing to gets deleted.
17th Nov 2016, 5:35 AM
scott johnson
scott johnson - avatar
0
okey thanks...
17th Nov 2016, 8:12 AM
Pranjal Upadhyay
Pranjal Upadhyay - avatar