Dangling Pointer In C [Explained With Examples]
Hello friends, in today’s article we are going to talk about Dangling Pointer In C.
But Before knowing about dangling pointers, you need to be aware of some basic things. So let’s first know about those basic things.
- When the program starts, the program gets some memory in the RAM, and then when the functions inside the program are called, then these functions get some memory of the memory space given to the program in the memory, as the function of these functions ends, the memory received by them also gets deallocated.
- Just as memory is allocated to functions, in the same way, memory is also allocated to variables.
- The memory space allocated to the variable lasts only for the life of the variable, as soon as the variable is nested, the memory space allocated to it also gets deallocated.
- The life of a variable is limited only to the block or function in which it is declared.
Let’s now understand what is a dangling pointer In C
What Is Dangling Pointer In C
A Dangling Pointer is a pointer that points to an illegal (deleted or freed) memory address.
Pointing to an illegal address by a dangling pointer can cause the program to crash, so while creating the program, keep in mind that no pointer should point to an illegal address.
Let’s understand the dangling pointer better through a program.
Example Program of Dangling Pointer
#include<stdio.h>
void fun(void);
int main()
{
fun();
return (0);
}
void fun()
{
int *p;
{
int x;
p = &x;
}
}
Take a look at this program carefully! In this program, we have called a function named fun() inside the main() function, which we have declared above the main() function and defined it below the main() function.
As soon as the main() function enters the program, its execution starts. Then it calls fun() function and fun() function gets some space in memory and pointer variable p created inside fun() function whose life is equal to the life of the fun() function.
There is another block inside this fun() function inside which there is a variable named x whose address we have stored in pointer p.
The life of this x variable is limited only to the block in which it is declared, so as soon as the program exits that block, the x variable is destroyed and the memory address assigned to the x variable is deallocated as soon as the x variable is destroyed. |
Since the address assigned to the x variable is now deallocated, the pointer is now pointing to a memory address that does not exist of its own, so such a pointer is now called a dangling pointer.
These are some of the reasons for becoming a dangling pointer, which we should avoid while creating a program.
- Access a local variable outside of its lifetime
- De-allocation of memory
- Function Call
1) Access a local variable outside of its lifetime
As we know, the lifetime of a variable is limited only to the block in which it is declared.
Since the variable’s life is limited, the variable is destroyed as soon as the program’s control exits that block (the block in which the variable was declared) and as soon as the variable is destroyed, the variable’s memory space also goes to de-allocated or free
But if a pointer is already referencing that variable, then it is now referring to an illegal address because the variable found is free as the variable is destroyed. In such a situation, now this pointer has become a dangling pointer that is pointing to an illegal address.
Example -:
#include <stdio.h>
int main()
{
int *p;
{
//block
int num = 27;
p = #
}
//here p is dangling pointer
return 0;
}
2) De-allocation of memory
Let’s understand this with an example
Example -:
// 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;
return 0;
}
A dangling pointer is created when a pointer points to a freed memory block. In this example, the memory obtained by the ptr pointer was released by the free() function, so the ptr pointer was pointing to a memory block that is now free.
3) Function Call
Let’s understand this also with an example -:
// 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;
}
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 this program, the pointer p calls a fun() function which returns the address of the local variable x. And as we know the scope of a local variable is limited only to the block in which it is declared, so here p is a dangling pointer because it is pointing to such an address of x that will destroy as soon as the function execution is over
How to Avoid the Dangling Pointer Errors
Errors like dangling pointer can be avoided by initializing NULL value in the pointer. If we assign a NULL value to the pointer, then the pointer will not point to the de-allocated memory block. Assigning a NULL value to a pointer means that the pointer is not pointing to a memory location.
Example -:
// 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;
}
Conclusion
Friends, I hope you have found the answer to your question and you will not have to search about what is Dangling Pointer in c language.
However, if you want any information related to this post or related to programming language, or computer science, then comment below I will clear your all doubts
If you want a complete tutorial on C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about Dangling Pointer In C
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.