pointers that do not point to a valid object of the appropriate type. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. As the system may reallocate the previously freed memory to another process, if the original program then dereferences the (now) dangling pointer, unpredictable behavior may result, as the memory may now contain completely different data. This is especially the case if the program writes data to memory pointed by a dangling pointer, as silent corruption of unrelated data may result, leading to subtle bugs that can be extremely difficult to find, or cause segmentation faults (*NIX) or general protection faults (Windows).

1 User has rated as useful.
Login to rate this answer.
Let us understand through the following code snippet
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BLOCKSIZE 20;
int main()
{
char *ptr;
ptr=(char *)malloc(BLOCKSIZE);
if(ptr!=NULL)
strcpy(ptr,"mohin khan");
printf("content=%s",ptr);
printf("naddress=%u",ptr);
free(ptr);
printf("nAfter free");
printf("ncontent=%s",ptr);
printf("naddress=%u",ptr);
}
output of the above code
content=mohin khan
address=134520840 //(just a example)
After free
content=
address=134520840 //same as the previous address
so we can conclude from the above code that
after free() also i keeps the address but content is deallocated.so now it is not pointing to any valid memory location.Hence ptr is now dangling pointer which probably having a address but not pointing to any valid memory location.
so it is good practice to assign NULL after freeing the allocated memory as below
ptr=NULL;
in this way dangling problem will be solved.
Login to rate this answer.
ishwar
Answered On : Oct 18th, 2011
Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory.
Login to rate this answer.
SURESH KUMAR KOLLIMALLA
Answered On : May 10th, 2013
By using dangling pointer we can access the values at the deallocated memory here, in the below code the memory allocated for variable n will be dealloeated as and when the control transfers from function hi() to main() but with the help of DPtr we can access the value of n.
Code
void main ( )
{
int *DPtr;
DPtr=hi();
}
int* hi( )
{
int n = 5;
return &n; }
Login to rate this answer.