Dangling pointer

What is dangling pointer ?

In programming dangling pointer is pointer which doesn’t point to valid object or memory. Dangling pointer arises when some memory is deallocated or deleted and pointing pointers value is not modified. So pointing pointer still points to the deleted memory address which is invalid. This pointer is known as dangling pointer. When some process tries to modify the address which is pointed by dangling pointer it causes segmentation fault in your program.


Causes of dangling pointer :

1. when global variable points the some variable in function or local block.

Solution :

initialize ptr=NULL immediate after block completion

2. Deallocating dynamic memory using free()

Solution :

After deallocation  of memory, immediate initialize pointer to NULL.

3. declaration of pointer

Solution :