Dangling pointer problem in c programming


Different types of pointers:

1. Dangling pointer:

If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.
Initially:


Later:

For example:


(q)What will be output of following c program?

#include<stdio.h>

int *call();
void main(){

int *ptr;
ptr=call();

fflush(stdin);
printf("%d",*ptr);

}
int * call(){

int x=25;
++x;

return &x;
}


Output: Garbage value
Note: In some compiler you may get warning message returning address of local variable or temporary

Explanation: variable x is local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and pointer is still pointing ptr is still pointing to that location.

Solution of this problem: Make the variable x is as static variable.
In other word we can say a pointer whose pointing object has been deleted is called dangling pointer.

#include<stdio.h>

int *call();
void main(){

int *ptr;
ptr=call();

fflush(stdin);
printf("%d",*ptr);

}
int * call(){

static int x=25;
++x;

return &x;
}

Output: 26

Generic pointer
Null pointer
Wild pointer
Dangling pointer
Near pointer
Far pointer
Graphics video memory
Text video memory
Huge pointer
Memory model in C
C tutorial

27 comments:

ankit said...

thank you for such good information

pavan said...

very nice information i got from this


pavankumar

prashant200288 said...

so well explained yaar........gr8

NIDHI said...

veryyy good

Vaibhav said...

hmmm really nicely explained ..............great

Nitssss......... said...

gr88 xplanation mahn!! keep on providing us with such valuable posts.....

RAJESH said...

thank you for explanation..
really i dont know before about this dangling pointer. now i clear..

Kalyani said...

nice but if u give more explanation for that then it is helpfull

George said...

Superbbbb

Manish Kumar said...

easy to understand and nice expalnation can't forget for my life.

dhana said...

wow excellent explanation given for gangling problem...)

b@lu said...

good example...useful for job inerview

parameswari said...

thnk u so much for giving this nice explanation on pointers concept in c

vamc said...

clear cut explanation... with good example. thank you..

Tuhin_d_Big said...

very nice and composed explanation...thanks a lot

K@RTiK said...

Nice work.
But why it gives me 26 as output which is correct.

gourab said...

I have gone through many dangling pointer example....
But dis one is simply D BEST..........

sudhanshu said...

well teached ,,,,,thank u very much

sjagram said...

nice vry nice

°ღ•ĶÁÚŚℋℐĶ●•٠·˙ said...

good effort sir! keep providing such useful info. ..

himanshu said...

excellent yar

chandrasekhar said...

ya it wont print garbage value it print output as 26

saurabh said...

showin warning along with 26 as output , rathar than garbage value ... chckd on DEV-C and tubro c

chandan said...

thank you.... this concept was asked in microsoft apti test....

little said...

Amit Prakash:thanks for good definition about dangling pointer,it was asked in Safran company technical round

anokha said...

easily understandable.

Arindam said...

#include
#include
int *call();
int x=25;
void main()
{

int *ptr=NULL;
clrscr();
ptr=call();
printf("\nOutside function call address of ptr = %u",ptr);
fflush(stdin);
printf("\nValue at ptr %d",*ptr);
}
int *call()
{
++x;
printf("\nInside function call address of x = %u",&x);
return &x;
}

Here, my point is : if I declare the variable x Global then we can easily get the value of it even in the main() even after flushing it.
So, my point is : in the earlier example it was not shown in main because the variable x was local to the function "call()", NOT for it was acting like a Dangling or so.