MEMORY VIOLATIONS
Dangling Pointers and wild pointers are among the few causes of memory safety violations.C and C++ provides with one of the most powerful tool programming world ever came with and this was Pointers but this gave programmer immense control over memory addresses and sometimes we try to acces memory which we are not suppose to access and this is called FAULT SEGMENT
Above we mentioned two types of memory violations-Dangling and Wild.
Dangling Pointers arises when some object is deleted or relocated or when some memory is made free without making pointer variable null. This happens because even after deletion pointer points to the same location in memory even though the reference has been deleted or may be getting used for some other purposes
For example
void func()
{
char *dp=malloc(1000)
free(dp); //here dp becomes dangling pointer
dp=NULL// now pd is null pointer and no longer a dangling pointer
}
Interesting thing about dangling pointers
char ch='a';
char *c=NULL;
c=&ch;
free(c);
printf("%c",*c);
It will still print the a because it still pointing to same memory location
Wild pointers-:
Main cause of it is not initializing the pointer variable to null when it is initially declared thus every pointer which has not been explicitly initializes is considered as a Wild pointers
For example
int f(int i)
{
char *dp//wild pointers
static char *scp//not a wild pointer because static initializes it to 0
}
SMART POINTERS
its a type of abstract data type and provides the additional features to the pointers such as automatic garbage collection and bound checking and is very useful in removing Dangling pointers.They set the pointers automatically to null when ther object is deleted. Java and C# dosent requires any smart pointers because they have there own garbage collection techniques
LISP was the first language having garbage collection
No comments:
Post a Comment