Dangling Pointers in C

Dangling Pointers in C
Techiio-author
Written by Shuvhojit DebFebruary 6, 2022
12 min read
C
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

Today we'll learn what is dangling pointer is and its work.

Dangling pointers in C

A pointer is a variable that stores the memory address of other variables. The pointers may also store the address of other’s memory items. They are of great help when it comes to dynamic memory allocation. A dangling pointer, on the other hand, points to a non-existential variable or a memory location. The dangling pointer may point to a variable or a memory location that might have been freed.

These pointers come into existence when the object or a variable or a memory location has been deleted or deallocated without modifying the value of that particular pointer.

In the C standard involving C pointers, many common bugs are occurred due to the pointers and managing the dangling pointers. Often, a programmer or a developer might fail to initialize the pointer with a valid address. That type of initialized pointer is known as a dangling pointer in C.

Dangling pointers mostly happen during the destruction of an object when it is being deleted or deallocated from memory without modifying the value of the pointer.

The dangling pointer can always point to a variable or memory location, consisting of the entire program code or the code of that particular operating system. If any value has been assigned to the dangling pointer, then it will overwrite that variable’s value of the program code or the operating system; in cases like such, the program will always show the undesirable result or even the crash.

If the memory is then reallocated in some other code, then the dereference of the dangling pointer will happen because of the segmentation fault. Since the value of the dangling pointer will not be modified at any time, it may still point to the memory locating of the other deallocated memory.

How Dangling Pointers Works in C?

The hanging pointers are analogous pointer just like the normal pointer but it works by taking consideration of ade-allocated object/ deleted object. It's nothing but a pointer which actually going to point a specific memory position that's actually deleted and it's called a hanging pointer.

The hanging pointer’s crimes can only be avoided just by initializing the pointer to one NULLvalue.However, also that pointer won't at each point to the demanded deallocated memory, If we try assigning the NULL value to a specific pointer. Assigning the NULL value to the specific pointer helps the pointer not pointing to any specific memory position.

Forde-allocating memory of the C swinging pointer conception, free () function is used with a single parameter just to make a pointer into a hanging pointer. This is how the hanging pointer will be created with free () function in the C rendering language. There's also another way of creating a hanging pointer. It's variable go out of the compass way of creating a hanging pointer conception.

The Swinging Pointers works just by pointing to the specific memory position which actually contains either some programming law or some law of the operating system. If we accept some value to the specific pointer also it'll overwrite the value of the program law.

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.

Syntax:

#include <stdlib.h>
#include <stdio.h>
int main()
{
int *ptr1 = (int *)malloc(sizeof(int));
free(ptr1);
ptr1 = NULL;
}

Output:

blogpost

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.

Syntax:

#include<stdio.h>
int *fun1()
{
int x1 = 5
return &x1;
}
int main()
{
int *p1 = fun1();
fflush(stdin);
printf("%d", *p1);
return 0;
}

Output:

blogpost

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.

Syntax:

#include<stdio.h>
int *fun11()
{
static int x11 = 5;
return &x11;
}
int main()
{
int *p11 = fun11();
fflush(stdin);
printf("%d", *p11);
return 0;
}

Output:

blogpost

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.

Syntax:

#include<stdio.h>
int main()
{
char *str1;
{
char a1 = ?A1?;
str1 = &a1;
}
printf("%s", *str1);
}

Output:

blogpost

Conclusion

I hope you understand what's the description of C Dangling/ Wild Pointers along with its syntax and explanation, how the hanging pointers work in C Programming Language along with colorful exemplifications of enforcing better and so fluently.

C Language
Programming
Dangling Pointers
Pointers
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance