top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

In C or C++ What is a dangling pointer?

+3 votes
124 views
In C or C++ What is a dangling pointer?
posted Dec 29, 2014 by Emran

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

A dangling pointer points to memory that has already been freed. The storage has been freed or variable been assigned the memory has it's life time ended, in other words storage no longer allocated. Now a pointer which is still holding the address of the same memory location which has been freed tries to access it causes segmentation fault to happen.

For example:-

int *x=(int *) malloc(4);
free(x);
*x=10; //THIS will cause the segmentation fault

answer Dec 29, 2014 by Prakash
0 votes

A dangling pointer arises when you use the address of an object after its lifetime is over. This may occur in situations like returning addresses of the automatic variables from a function or using the address of the memory block after it is freed.

answer Dec 30, 2014 by Mohammed Hussain
...