Header Ads

Dangling Pointer

Dangling Pointer


Dangling pointer are the pointer which contain address of no "memory blocks."
In other words dangling pointer arises when the variable are address it is pointing to is freed.
or,
A pointer pointing to non-existing memory location is called dangling pointer.
Dangling pointer points to which is not valid anymore in the program.

How to prevent dangling pointer generation:--

  •  Allocate the pointer another object before deleting the object it is pointing currently.
  •  Don't delete the objects(not a good practice lead to memory blockage though)
 Example

main(){

      int *p = malloc(sizeof(int));

      free (p);   /* now the p becomes dangling pointer */

}

No comments