Dangling Pointer in C

Here you will find out about dangling reference and dangling pointer in C.

In PC programming, memory is allotted for holding information object. After the

work is done the memory is deallocated with the goal that this memory can be utilized to store some other information object.

What is Dangling Pointer?

While programming, we use pointers that contain memory locations of information objects.

A dangling pointer is a pointer that focuses on the memory area significantly after its deallocation.

Or then again we can say that it is a pointer that doesn’t point to a substantial information object of the fitting sort.

The memory area pointed by dangling pointer is known as a dangling reference.

Presently in the event that we get to the information put away at that memory area

utilizing the dangling pointer then it will bring about program crash or erratic conduct.

We should take an example to get this.

Reason for Dangling Pointer in C

void function(){
	int *ptr = (int *)malloc(SIZE);
	. . . . . .
	. . . . . . 
	free(ptr);	//ptr now becomes dangling pointer which is pointing to dangling reference

In above example we previously assigned a memory and put away its location in ptr.

Subsequent to executing hardly any announcements we deallocated the memory.

Presently still ptr is indicating same memory address so it moves toward becoming dangling pointer.

I have referenced just a single situation of dangling pointer.

There are numerous different circumstances that causes dangling pointer issue.

How to Solve Dangling Pointer Problem in C?

To tackle this issue simply allot NULL to the pointer after the deallocation of memory that it was pointing.

It implies now pointer isn’t indicating any memory address.

Let’s see an example.

On the off chance that you have any questions identified with above dangling pointer in C tutorial

at that point don’t hesitate to ask by remarking beneath.

Recent Posts

error: Alert: Content is protected!!