If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while a pointer is still pointing such memory location. Such a pointer is known as a dangling pointer and this problem is known as the dangling pointer problem.
Example:
Output:
Segmentation fault (core dumped)
Explanation:
Variable x is a local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and the pointer is still pointing ptr is still pointing to that location
In other words, we can say a pointer whose pointing object has been deleted is called a dangling pointer.
The solution to this problem: Make the variable x is as a static variable.
No comments:
Post a Comment