0
What is Dangling Pointer?
Can anyone please explain what Dangling Pointer is? How can that be avoided ? Any example is possible.
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.
+ 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.
+ 1
Assign nullptr to pointers not pointing to anything, not NULL. NULL is outdated when it comes to pointers; mainly used in C.
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. ??
0
the data that the pointer is pointing to gets deleted.
0
okey thanks...