What is dangling pointer?please explain with a suitable example!
Dangling pointers are pointers that do not point to a valid object of theappropriate type, like you deleted what it points at.
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 dangling pointer,
unpredictable behavior may result, as the memory may now contain completely
different data.
A straightforward example is shown below:
{
char *dp = NULL;
/* ... */
{
char c;
dp = &c;
}
/* c falls out of scope */
/* dp is now a dangling pointer */
}
A solution to the above is to assign NULL to cp immediately before the inner block
is exited. Another solution would be to somehow guarantee cp is not used again
without further initialisation.
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Learn more about our help with Assignments:
Adobe Flash