Tuesday, 29 January 2019

Dangling pointer | Causes of Dangling pointer | How to avoid it


A pointer in the C programming language plays a very important role, but if we are not used pointer properly then they create some crucial issues. In this article, we will discuss the bad effect of pointers. Before start discussion on the dangling pointer, let's take a look on, how the pointer is allocated in memory?


Dangling Pointer
Figure 1: Pointer points an allocated memory




















What is Dangling pointer?


A dangling pointer is created when data is deleted from memory but pointer still points to that memory location (as shown in Figure 2). An issue is created because pointer still pointing to the memory which is not available. When the programmer tries to dereference the dangling pointers that it shows undefined behaviour and can be the cause of the segmentation fault. 

A pointer (dangling pointer) which is not pointing to the valid memory location can be the reason of undefined behaviour.



Dangling pointer
Figure 2 Dangling pointer

























In the above figure, pointer x and pointer y pointing to valid memory data but pointer z is pointing to a memory which is deallocated. So, pointer z becomes a dangling pointer. but when you will try to access the pointer z then you will get the undefined error. 



Causes of Dangling pointer


There a lot of reason due to the dangling pointer is created. In this section, we will discuss the causes of a dangling pointer.

1. Out of scope


When a local variable is declared or initialized within a block then memory is allocated to the local variable.  That memory is deleted or freed when it comes out of the block. 

#include <stdio.h>
int main()
{
    int *p;

    //block
    int a =10;
    p = &a;
    }
    {
        printf ("a=%d\n", *p);  //Here, p is a dangling pointer
    }
    return 0;
}

2. De-allocation of memory


In the C programming language, malloc, calloc and realloc library function are allocates memory during run time and free is used to de-allocate memory. Combat combination of malloc, calloc, realloc and free generate the dangling pointer. Whenever the user tries to access the freed memory then you will get the segmentation fault. 


#include <stdio.h>
int main ()
{
    int *p = (int*) malloc (sizeof (int));
    free (p);  //free the allocated memory
    *p = 20 // p is dangling pointer
    p = NULL //No more dangling pointer
    return 0;
}



How to avoid Dangling pointer?


The generation of the dangling pointer is avoided by user or programmer if they use NULL explicitly after freeing the dynamic memory.


#include <stdio.h>
int main ()
{
    int *= (int*) malloc (sizeof (int));
    free (p);  //free the allocated memory
    p = NULL //No more dangling pointer
    return 0;
}


Another factor which creates the dangling pointer is to not return the address of the local variable from the function. This can be solved if we are using the static variable because the lifetime of the static variable is throughout the run of the program.


#include<stdio.h>
int *checkstaticfun( )
{
 static int a = 9;
 return &a;
}
int main ( )
{
int *p = NULL;
p = checkstaticfun( );
 // Now p is Not a dangling pointer as it points to static variable.
printf ("%d",*p);
return 0;




In this article, I tried to clear all the concept related to a dangling pointer, but I would love to know about your views. So, Please write your comment in the comment box.



Recommended Posts


























No comments:

Post a Comment