Learn Dangling Pointer in C


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


Dangling Pointer
Figure 1: Pointer points an allocated memory


What is Dangling pointer?


Most of the people ask a question is that what is the difference between a wild pointer and Dangling-pointer? When a pointer is not initialized with any address is known as a wild pointer. And 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 it still pointing to the memory which is not available when the programmer tries to dereference the dangling-pointers that it shows undefined behavior and can be the cause of the segmentation fault.

See Also: Learn Pointer to Pointer in C

A pointer which is not pointing to the valid memory location can be the reason of undefined behavior.

Dangling pointer

In the above figure, pointer x and pointer y pointing to valid memory data but pointer z are pointing to a memory which is deallocated. So, pointer z becomes a dangling-pointer. But when you 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.

1. Function Call

The pointer becomes dangling if the local variable is not static.

#include <stdio.h>
int *fun(); //Declaration of function fun()
int main()
{
int *ptr=fun();
printf ("%d\n", *ptr);
return 0;
}
//Definition of function fun()
int *fun()
{
int a =10;
return &a;
}

Output
Garbage Address

Pointer 'ptr' doesn't become dangling if 'a' is static.

#include <stdio.h>
int *fun(); //Declaration of function fun()
int main()
{
int *ptr=fun();
printf ("%d\n", *ptr);
return 0;
}
//Definition of function fun()
int *fun()
{
static int a =10;
return &a;
}

Output
10

2. 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;
}

3. 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 the 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. It 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 a 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.


























Learn Dangling Pointer in C Learn Dangling Pointer in C Reviewed by Glory on 04:00 Rating: 5

No comments:

Powered by Blogger.