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?
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.
int *call();
void main(){
int *ptr;
ptr=call();
fflush(stdin);
printf("%d",*ptr);
}
int * call(){
static int x=25;
++x;
return &x;
}
Output: 26
Null pointer
Wild pointer
Dangling pointer
Near pointer
Far pointer
Graphics video memory
Text video memory
Huge pointer
Memory model in C
C tutorial
22 comments:
so well explained yaar........gr8
veryyy good
hmmm really nicely explained ..............great
gr88 xplanation mahn!! keep on providing us with such valuable posts.....
nice but if u give more explanation for that then it is helpfull
Superbbbb
easy to understand and nice expalnation can't forget for my life.
wow excellent explanation given for gangling problem...)
good example...useful for job inerview
thnk u so much for giving this nice explanation on pointers concept in c
very nice and composed explanation...thanks a lot
Nice work.
But why it gives me 26 as output which is correct.
I have gone through many dangling pointer example....
But dis one is simply D BEST..........
well teached ,,,,,thank u very much
nice vry nice
good effort sir! keep providing such useful info. ..
excellent yar
ya it wont print garbage value it print output as 26
showin warning along with 26 as output , rathar than garbage value ... chckd on DEV-C and tubro c
Amit Prakash:thanks for good definition about dangling pointer,it was asked in Safran company technical round
easily understandable.
#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.
Post a Comment