To recall – Pointers variables store the address of other variables. Pointers hold a great significance, but there are certain types of pointers, and not all pointers are healthy.
There are some bad pointers, one of which is the Dangling pointer in C. Let us read something more about it.
What is Dangling Pointer in C?
Dangling pointers are originated whenever an object is de-allocated or deleted. But this is done without modifying the value of the pointer. So the pointer still exists while pointing to the memory which is now de-allocated. This pointer is known as Dangling Pointer, and it is of no use.
- String in C Programming
- Own Implementation of Sizeof and Strlen in C
- Compare Two Strings Without Using Strcmp in C
How is Dangling Pointers Created?
Just like is mentioned in the above paragraph, dangling pointers are generated when memory is freed or deallocated.
1. Local Variable is Returned in Function Call
#include<stdio.h>
#include<string.h>
char *prntHeyy()
{
char string1[20];
strcpy(string1,”HEYY!!”);
return(string1);
}
int main()
{
printf(“%s”, prntHeyy());
}
Output
(null)
A function is created to print Heyy. But when it is called in the main function, str goes out of scope. Str is not declared as static, so its scope is not beyond the function.
2. When Variable is not in Scope
#include<stdio.h>
int main()
{
char **stringptr;
{
char *string1 = “Heyy!”;
stringptr = &string1;
}
printf(“%s”, *stringptr);
}
Here, the scope of the string1 character is limited to the block, and as soon as the block ends, the scope ends too. Hence, the pointer becomes a dangling pointer.
3. When Memory is De-allocated
#include<stdio.h>
#include<stdlib.h>
int main()
{
char **stringptr;
char *string1 = “Heyy!”;
stringptr = &string1;
free(string1);
printf(“%s”, *stringptr);
}
Output
*** Error in `./a.out’: munmap_chunk(): invalid pointer: 0x0000000000400654 ***
Aborted (core dumped)
Here, the double pointer is pointing to the single pointer string1. But when the single pointer is freed, the double-pointer becomes dangling pointers
How to Avoid Dangling Pointer in C?
After the memory has been un-allocated, the pointer should then be initialized to ‘Null’. This will prevent the pointer from dangling because it will then not point to any memory location.
What is the Difference Between Wild Pointer & Dangling Pointer?
The wild pointer is the one that has not been initialized, so it points to no memory location and stores a garbage value by default.
While a dangling pointer is a pointer that points to a memory location that has been de-allocated, so it does not store a garbage value, but gives the error and creates trouble in the program.
Also Read,
- Memory Layout of C Program
- Types of Preprocessor Directives in C Language
- How Dangling Pointer Affects Programming?
- What is Double Pointer in C?
- What is Pointers in C?
- How String works in C?
- How to implement Sizeof and Strlen in C?
- How to compare two strings without using strcmp?
- What is the use of Operator Precedence in C?
- What is Storage Classes in C Language?
- What is C Tokens?
- What is Operators in C?