Dangling pointer
When a pointer pointing to a memory location that has been already deleted means freed is called a dangling pointer.
There are three different ways where Pointer act as dangling pointers.
First Way:
De-Allocation Memory:
// Deallocating a memory pointed by ptr causes // dangling pointer #include <stdlib.h> #include <stdio.h> int main() { int *ptr = (int *)malloc(sizeof(int)); // After below free call, ptr becomes a // dangling pointer free(ptr); // No more a dangling pointer ptr = NULL; }
Second Way:
Function Call:
// The pointer pointing to local variable becomes // dangling when local variable is not static. #include<stdio.h> int *fun() { // x is local variable and goes out of // scope after an execution of fun() is // over. int x = 5; return &x; } // Driver Code int main() { int *p = fun(); fflush(stdin); // p points to something which is not // valid anymore printf("%d", *p); return 0; }
Output:
A garbage Address
In the above program doesn’t appear or p didn’t become dangling if x is a static variable.
// The pointer pointing to local variable doesn't // become dangling when local variable is static. #include<stdio.h> int *fun() { // x now has scope throughout the program static int x = 5; return &x; } int main() { int *p = fun(); fflush(stdin); // Not a dangling pointer as it points // to static variable. printf("%d",*p); }
Output:
5
Third Ways:
Variable Goes Out Of Scope:
void main()
{
int *ptr;
.....
.....
{
int ch;
ptr = &ch;
}
.....
// Here ptr is dangling pointer
}
Void Pointer:
A void * is a specific pointer type void, that points a some data location in storage, means which doesn’t have a specific type.
Generally void refers to a type.
The void the type of data to point it any type.
If we assign the address of char data-type to void pointer it will become char pointer, if int data-type then int pointer and so on.
Any pointer type is convertible to void pointer hence it can point to any value.
The void pointers can’t be dereferenced type.
Pointer arithmetic is not possible on pointers of void due to lack of concrete value and size.
#include<stdlib.h> int main() { int x = 6; float y = 8.5; //A void pointer void *ptr; ptr = &x; // (int*)ptr - does type casting of void // *((int*)ptr) dereferences the typecasted // void pointer variable. printf("Integer variable is = %d", *( (int*) ptr) ); // void pointer is now float ptr = &y; printf("\nFloat variable is= %f", *( (float*) ptr) ); return 0; }
Output:
Integer variable is = 6
Float variable is= 8.500000
Null Pointer:
Null pointer is a pointer to point nothing.
If we don’t have any address to assign then the pointer is a initialize with NULL.
#include <stdio.h> int main() { // Null Pointer int *ptr = NULL; printf("The value of ptr is %p", ptr); return 0; }
Output:
The Value of ptr is Nil
Some Important points of Null Pointer:
- Null vs Uninitialized Pointer: The Null pointer is initialized by a NULL value. Whereas Uninitialized Pointer didn’t initialize any member and type means undefined value.
- Null vs Void Pointer: Null Pointer is a value and void pointer is a type.
Wild Pointer:
A pointer which has not been initialized to anything not even null is know as wild pointer.
The pointer may be initialized to a non-NULL garbage value that may not be a valid address.
int main() { int *p; /* wild pointer */ int x = 10; // p is not a wild pointer now p = &x; return 0; }
If you find any issue on this article and interested to rewrite it then please contact us.