Dangling pointers and wild pointers in computer programming are pointers that do not point to a valid object of the appropriate type.
In many applications memory is allocated for holding data objects. After using these objects, tha aplication will de-allocate this memory so that the memory can be re-used. In some cases the alications may use a pointer to an object whose memory is already de-allocated. This may lead to application crash or an unpredictable behavior.
scenarios which leads to dangling pointer
The most common result of this bug is the crash of the application or its running thread.
Examle 1:
1 2 3 4 5 6 7 8 9 | #include "stdlib.h" void func() { char *dp = malloc(A_CONST); /* ... */ free(dp); /* dp now becomes a dangling pointer */ /* ... */ } |
Example 2:
1 2 3 4 5 6 7 8 9 | { char *dp = NULL; /* ... */ { char c; dp = &c; } /* c falls out of scope */ /* dp is now a dangling pointer */ } |
Example 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include "stdio.h" int *call(); void main(){ int *ptr; ptr=call(); fflush(stdin); printf("%d",*ptr); } int * call(){ int x=25; ++x; return &x; } |
Description :
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
#Home #Sitemap #Resources #Terms of Use
Copyright©2012 electrofriends.com All Rights Reserved
Contact:info@electrofriends.com