How Dangling Pointer in C Affects Your Programming ?

To recall – Pointers variables store the address of other variables. Pointers hold 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.

Also Read: How to Dereference a Void Pointer in C?

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.

Also Read: What is Double Pointer 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 static, so its scope is not beyond the function.

2. When the Variable is not in the 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 unallocated, 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.

Hello, My Name is Abhinav. I am an Author in the Education Category of Trickyedu. I have Done My Engineering in Computer Science from DIT University. I have a good command on Science, Programming Language, and microprocessors. So, I choose this platform to share my knowledge and experience.

Leave a Comment