Dangling Pointer in C Programming

By | October 6, 2017

Let us learn about dangling pointer in C programming and understand how it is implemented by an example, explanation and much more.

What is a Dangling Pointer?

A dangling pointer points to a memory location that is free. The storage is, therefore, unallocated. If you try to access it, you might encounter segmentation fault error.

Dangling pointers occurs when an object is deleted or de-allocated, without modifying the value of the pointer. As a result, the pointer is still going to point to the de-allocated memory location.

Once the memory is de-allocated, the system may reallocate that block of freed memory to another process.

Understand Dangling pointer in C Programming with Example, solutions, avoid dangling pointer and much more

Image Source: Wikimedia

In case the program then dereferences the (now) dangling pointer, the unpredictable behaviour may result, as the memory may now contain completely different data.

This problem can become worse when the program writes data to the memory pointed by a dangling pointer causing a silent corruption of unrelated data, leading to subtle bugs that can be extremely difficult to find.

Moreover, if the overwritten data is bookkeeping data used by the system’s memory allocator, the corruption can even cause system instabilities.

Hence, dangling pointer problem occurs when the pointer still points to the same location in memory even though the reference has been deleted and may now be used for other purposes.

A common mistake that we often do in C programming is to return the address of a stack-allocated local variable.

We know that once a called function returns, the space for these variables gets de-allocated and technically they have garbage values.

Let us glance over the following code to see how a dangling pointer occurs when a called function returns. But, before that, you must know everything about a function pointer in C programming.

Note: These illustrations of dangling pointers in C programming are compiled with GNU GCC compiler using CodeLite IDE on Microsoft Windows 10 operating system.

 

Illustration of Dangling Pointer in C Programming

The above program returns the address of the variable ch. So, the calling function may access its value.

Any functions called thereafter will overwrite the stack storage allocated for ch with other values and the pointer would no longer work correctly. Therefore, if a pointer to ch must be returned, it must be declared as static.

Possible Scenarios of Dangling Pointer in C Programming

To sum up, everything, let us see the possible cases where a dangling pointer can be encountered. One important pointer to note is that there is a difference between dangling pointer and null pointer.

Case 1: Declaring the pointer variable but not initializing it

Case 2: De-allocating dynamic memory using free() function

How To Avoid Dangling Pointer Problem?

Let us first see an example of another dangling pointer problem and then let us see what are the solutions that can help us to avoid it.

Illustration of Dangling Pointer in C Programming

As you can see above, a becomes a dangling pointer now.

Solution 1:

The first solution to the above issue is to assign 0(null) to a immediately before exiting the block in which it is declared.

Solution 2:

The alternative approach would be to somehow ensure that a won’t be used again without further initialization.

 

Solution 3:

Another solution is to make use of smart pointers which performs automatic memory management and bounds checking.

Solution 4:

There are tools like Valgrind that enables you to automatically detect many memory management and threading bugs which can be used to detect cases of dangling pointer problems.

If you have any doubts about dangling pointer in C programming, let us know about it in the comment section.

MORE ON POINTERS
Wild Pointers
Huge Pointers
Null Pointers
Void Pointers
Function Pointers
Near Pointers
Far Pointers

Let's Discuss