What is a dangling pointer?
Ans:
Suppose we allocate a chunk of memory and store its address in a pointer. If this chunk of memory is freed and if the pointer continues to point to that location, the pointer is said to be a dangling pointer.
Example:
#include<iostream.h>
void main()
{
int *p;
int *q=new int;
p=q;
delete q;
//code
}
Explanation:
Here the p and q are pointing to the same memory location.After delete q statement p becomes a dangling pointer.As p is pointing to the deleted memory location.
No comments:
Post a Comment