Dangling Pointer

Dangerous pointers are those that point to memory locations that have already been erased or released. During object deletion, dangling pointers are frequently created. When an object with an incoming reference is removed or de-allocated without changing the pointer’s value, this occurs. The pointer is still pointing at the deallocated memory’s address in memory. The system may reallocate the previously deleted memory by itself, which may lead to a number of unexpected outcomes because the memory may now contain different data. The following causes contribute to dangling pointers:

Releasing or deleting temporary memory

  • The pointer continues to point to freed space even after memory is deallocated. Here’s an illustration to show how that occurs:
#include <stdio.h>
int main()
{
    int a = 80;
    int *ptr = (int *)malloc(sizeof(int));
    ptr = &a;
    free(ptr);
    return 0;
}

The code shown above shows how to construct a variable pointer *ptr and an integer variable a with the value 80. The malloc() function is used to generate the pointer variable *ptr. Given that we are aware that the malloc() function produces a void, we type-convert the void pointer into an int pointer using the int * function.

Function Call

  • We will now examine how the function call causes the pointer to become dangling.
#include <stdio.h>
int *myvalue()
{
    int a = 10;
    return &a;
}

int main()
{
    int *ptr = myvalue();
    printf(“%d”, *ptr);
    return 0;
}

Output: Segmentation Fault!

The func() method’s return value is contained in the pointer ptr, which is declared in the main() function of the code above. The programme control is transferred to the context of the int *func when the function func() is called (). The address of the integer variable an is then returned by the method func().

The integer variable an is no longer accessible for the remainder of the program’s execution at this point, and programme control returns to the main() function. And because it points to a memory location that has been removed or freed from the stack, the pointer ptr becomes dangling. The application consequently causes a segmentation fault.

The outcome would have been 10 had the code been modified and the integer variable, which is static and is declared globally, been local rather than global.

The best way to prevent dangling pointer errors

Our applications get unpleasant defects from the hanging pointer, and these issues frequently lead to security flaws. These errors that result from the generation of a dangling pointer can be prevented by simply initialising the pointer value to NULL. The pointer won’t then point to the space in memory that was freed. While the purpose of giving the pointer the NULL value was to prevent it from pointing to any previously or arbitrarily designated memory location.

Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in