Dangling pointers in C

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.

Posted on by