Updated April 13, 2023
Introduction to Dangling Pointers in C
The C Dangling pointer is a type of pointer that actually points to a specific memory location that is to be free or deleted. There are some different ways where the pointer now acts as a dangling pointer. Most of the times there are only 3 different types/ways where the pointer will act as one of the dangling pointers. They are De-allocation of memory, Function Call, and Variable goes out of the scope. These dangling pointers are used when the memory management and pointers is having most of the common bugs. It usually appears/occurs at the object destruction time whenever the object deleted or de-allocated from memory without modifying pointer value. In this topic, we are going to learn about Dangling Pointers in C.
Syntax:
free(a1)
How Dangling Pointers Works in C?
The dangling pointers are similar pointer just like the normal pointer but it works by taking consideration of a de-allocated object/deleted object. It is nothing but a pointer which actually going to point a specific memory location that is actually deleted and it is called a dangling pointer.
The dangling pointer’s errors can only be avoided just by initializing the pointer to one NULL value. If we try assigning the NULL value to a specific pointer, then that pointer will not at all point to the needed deallocated memory. Assigning the NULL value to the specific pointer helps the pointer not pointing to any specific memory location.
For de-allocating memory of the C dangling pointer concept, free() function is used with a single parameter just to make a pointer into a dangling pointer. This is how the dangling pointer will be created with free() function in the C coding language. There is also another way of creating a dangling pointer. It is variable go out of the scope way of creating a dangling pointer concept.
The Dangling Pointers works just by pointing to the specific memory location which actually contains either some programming code or some code of the operating system. If we accept some value to the specific pointer then it will overwrite the value of the program code.
Examples of Dangling Pointers in C
Here are the following examples mention below:
Example #1
This is the example of the de-allocation of the memory of the C Programming Language by the specific ptr causes. At first, standard libraries or C language is included with the #include method/function. Then int main() is created to write C Code. The *ptr is created to get a pointer variable which is with the help of the malloc() function. The malloc() function usually returns the void value, so here int * is used to convert the void pointer to an int pointer. Then free() function with the parameter “ptr1” is used in order to make the pointer as a dangling pointer. So when the compiler completed the execution, the compiler will run the code perfectly but there will be no output because nothing is mentioned too in print to show as output in the command prompt.
Code:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int *ptr1 = (int *)malloc(sizeof(int));
free(ptr1);
ptr1 = NULL;
}
Output:
Example #2
This is the example of implementing the Function call way or representing the dangling pointer. Here one of the pointers which point to the local variable becomes into a dangling pointer when the local variable is not at all static. Here the pointer didn’t become into dangling pointer because the x1 is not defined as a static term. Here at first a pointer fun1() is created with a normal int variable with value 5. Then main() is created for entering the C code. Then pointer p1 is created to call the fun1(). Then after this, the pointer p will not point to a specific point, it points to the one which is not at all a valid one anymore. Then printf is used to print. But here there will be a warning when the c code runs in the c compiler. Check out the output so that you will know. Here in this example, the normal pointer doesn’t even become into a dangling pointer.
Code:
#include<stdio.h>
int *fun1()
{
int x1 = 5
return &x1;
}
int main()
{
int *p1 = fun1();
fflush(stdin);
printf("%d", *p1);
return 0;
}
Output:
Example #3
This is also an example of the function call which is similar to the above example. At first, usually #include is used for including the standard library. Then a function fun11() is created and included a static int variable “x11” with a value “5”. Then the main() function is used along with the pointer variable “P11” to include the pointer function fun11(). Then fflush() function is used. The fflush function is mostly used for output streams. Fflush(stdin) is a type of undefined behavior. Then printf() function is used to print the pointer variable which is nothing but the x11 variable value.
Code:
#include<stdio.h>
int *fun11()
{
static int x11 = 5;
return &x11;
}
int main()
{
int *p11 = fun11();
fflush(stdin);
printf("%d", *p11);
return 0;
}
Output:
Example #4
This is the example of implementing the variable which goes out of the scope. Here the variable will go out of the scope then the pointer pointing to the variable becomes into a dangling pointer. Here at first, we are declaring the pointer variable str1. Then inside we are declaring a character variable. Now the str1 variable contains the variable “a1”s address. Then control will come out of inner scope. Here a1 variable will no longer available. So str1 will point to a specific deallocated memory. It means the str1 pointer will become into a dangling pointer but A1 is an undeclared variable.
Code:
#include<stdio.h>
int main()
{
char *str1;
{
char a1 = ?A1?;
str1 = &a1;
}
printf("%s", *str1);
}
Output:
Conclusion
I hope you understand what is the definition of C Dangling/Wild Pointers along with its syntax and explanation, how the dangling pointers work in C Programming Language along with various examples of implementing better and so easily.
Recommended Articles
This is a guide to Dangling Pointers in C. Here we discuss how Dangling Pointers Works in C along with programming examples to understand better. You may also have a look at the following articles to learn more –