Dangling Pointers Avoid them Strictly!
This article takes off from an earlier one on constant pointers. Here, the focus is on dangling pointers—how they occur and how to prevent them. The article is a great guide for C newbies.
Dangling pointers are those in the C programming language that do not point to valid objects. Pointers are like sharp knives that have tremendous power in C programming. When used properly, they reduce the complexity of the programs to a great extent. But when not used properly, they result in adverse effects and, in the worst case scenario, may crash the system.
In this article, I mainly focus on dangling pointers and what causes them by looking at several different situations in which they occur. I also suggest simple methods to avoid them.
Note: Code snippets provided here are tested with the GCC compiler [gcc version 4.7.3] running under the Linux environment.
Let’s now look at three different cases that give rise to dangling pointers.
Case 1: When a function returns the address of the auto variables
Consider the simple code snippet given below (Code 1):
When we run the program shown in Code 1 in the GCC compiler, we get the warning shown in Figure 1.
What is wrong when the address of the auto/local variables is returned? Let us examine this in detail: 1. We know that whenever there is a function call, a new stack frame will be created automatically, where auto variable auto_var is local in our example. Its scope and lifetime is within the function call. 2. When the control returns from the function call, all the
memory allocated for that function will be freed automatically. 3. In our example program, we are returning the address of the auto variable and collecting this in the int_ptr pointer in the main function. So, int_ptr is still pointing to the memory, which is freed as mentioned in Point 2. 4. One can observe allocation and deallocation of the stack frame (let us called this fun frame) for the fun() in Figure 2 for better understanding. 5. Now, int_ptr becomes a dangling pointer. Dereferencing
this pointer results in unexpected output. 6. So, always take extra care while playing with pointers and local variables. Any attempt to dereference the pointer that is already dangling may still print the correct value after the control returning from a function call, but any functions called thereafter will overwrite the stack storage allocated for the auto_var variable with other values, and the pointer will no longer work correctly.
How to prevent a pointer from becoming a dangling pointer in this case: If a pointer to auto_var is to be returned, auto_var must have scope beyond the function so that it may be declared as static in order to avoid the pointer from dangling. This is because the memory allocated to the static variables is from the data segment, where the lifetime will be throughout the program.
Case 2: When the variable goes out of scope
Consider the sample code (Code 2) given below for analysis:
Running the program shown in Code 2 in the GCC compiler with the -Wall option results in the warning shown in Figure 3.
Since the variable var is invisible for the outer block shown in Code 2, iptr is still pointing to the same object even when the control comes out of the inner block. Hence, the pointer iptr becomes a dangling pointer after Line 10 in the example shown in Code 2.
Case 3: When, in dynamic memory allocation, the block of memory that is already freed is used
Consider the sample code given below:
Figure 2: Program memory and stack frames
Figure 1: Compiler generating warning