Dangling Pointers In C

Let's observe the following examples.

In the figure displayed above, it is evident that Pointer 3 is a dangling pointer. Pointer 1 and Pointer 2 are the pointers that point to the allocated objects, namely Object 1 and Object 2, respectively. Pointer 3, on the other hand, is a dangling pointer since it points to the deallocated object.

Let's explore the concept of a dangling pointer using a few C programming examples.

Using free function to de-allocate the memory.

Example

#include <stdio.h>
#include <stdlib.h>
int main()
{
   int *ptr=(int *)malloc(sizeof(int));
   int a=560;
   ptr=&a;
   free(ptr);
   return 0;
}

In the preceding code snippet, we initialized two variables, namely ptr and 'a', where 'ptr' represents a pointer while 'a' is an integer variable. The ptr variable is a pointer created using the malloc function. Since malloc returns void, we utilize int * to cast the void pointer into an integer pointer.

The code snippet int ptr=(int )malloc(sizeof(int)); reserves memory space of 4 bytes, as depicted in the following diagram:

The function free(ptr) releases the memory, represented by a crossed-out area in the image below, and the 'ptr' pointer becomes dangling since it now points to released memory.

If we set the 'ptr' to NULL, it ensures that 'ptr' does not point to any memory location. Consequently, we can confirm that 'ptr' is not a dangling pointer, as illustrated in the following diagram:

Variable goes out of the scope

When the variable's scope ends, the pointer referencing it turns into a dangling pointer.

Example

#include<stdio.h>
int main()
{
    char *str;
    {
        char a = ?A?;
        str = &a;
    }
    // a falls out of scope 
    // str is now a dangling pointer 
    printf("%s", *str);
}

In the above code, we did the following steps:

  • First, we declare the pointer variable named 'str'.
  • In the inner scope, we declare a character variable. The str pointer contains the address of the variable 'a'.
  • When the control comes out of the inner scope, 'a' variable will no longer be available, so str points to the de-allocated memory. It means that the str pointer becomes the dangling pointer.

Function call

Next, we'll explore the scenario where the pointer turns into a dangling pointer upon function invocation.

Let's understand through an example.

Example

#include <stdio.h>
    int *fun(){
    int y=10;
    return &y;
  }
int main()
{
int *p=fun();
printf("%d", *p);
return 0;
}

In the above code, we did the following steps:

  • First, we create the main function in which we have declared 'p' pointer that contains the return value of the fun .
  • When the fun is called, then the control moves to the context of the int *fun, the fun returns the address of the 'y' variable.
  • When control comes back to the context of the main function, it means the variable 'y' is no longer available. Therefore, we can say that the 'p' pointer is a dangling pointer as iLogic Practices to the de-allocated memory.

Output

Output

[Program Output]

Illustrate the functionality of the code above through a diagram.

Example

[Program Output]

Let's explore an additional instance of a dangling pointer.

Example

#include <stdio.h>
int *fun()
{
    static int y=10;
    return &y;
}
int main()
{
   int *p=fun();
   printf("%d", *p);
    return 0;
}

The aforementioned code shares similarities with the one before it, with the sole distinction being that the variable 'y' is declared as static. It's important to note that static variables are stored in the global memory.

Output

Output

[Program Output]

Now, we illustrate the functionality of the code mentioned above diagrammatically.

Example

[Program Output]

The diagram above illustrates the stack memory structure. Initially, the fun function is invoked, leading the execution flow to the context of the int fun. Since 'y' is a static variable, it resides in the global memory area, granting it visibility throughout the entire program. Upon the return of the address value, the control transitions back to the main context. The pointer 'p' holds the memory address of 'y', specifically 100. Upon dereferencing 'p', the output reflects the value of 'y', which is 10. Consequently, it can be affirmed that the pointer 'p' is not a dangling pointer since it encapsulates the address of a variable stored in the global memory space.

Avoiding Dangling Pointer Errors

Dangling pointer issues can be prevented by setting the pointer to NULL initially. By assigning NULL to the pointer, it will not point to any specific memory location. This practice ensures that the pointer does not point to deallocated memory. Setting a pointer to NULL indicates that it is not pointing to any memory location.

Input Required

This code uses input(). Please provide values below: