Dangling Pointer in C
In this program, you will know about dangling pointer and get the example code of dangling pointer in c programming.
What is a Dangling Pointer
A dangling pointer refers to a memory address that is no longer valid or has been de-allocated.
Dangling Pointer in C program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include<stdio.h> #include<malloc.h> int main() { int *ptr; //declaring a pointer //creating a dangling pointer ptr = (int*)malloc(sizeof(int)); //freeing the memory free(ptr); //accessing the freed memory *ptr = 10; printf("Value of ptr is: %d",*ptr); return 0; } |
Output
It will display a blank screen.
Check out our other C programming Examples