Type Here to Get Search Results !

Dangling Memory

Dangling Memory

Dangling memory is a memory location allocated to an object that cannot be referenced any more.
It is normally a result of a logical mistake in the code.

For example, while we are in a function, we may allocate arrays dynamically, use them and when we exit the function, the allocated memory will not be de-allocated there by leading to a dangling memory problem.

A pointer pointing to a non existing memory location is called a “Dangling Pointer”.
Dangling pointer is the pointer; when an object is de-allocated without modify the value of the pointer, even though the pointer still points to the memory location of the de-allocated memory. This may lead to an application crash or an unpredictable behaviour:
Scenario that cause dangling pointer:
Application makes use of an object after it has been released, and there by accessing to an invalid memory location.
A function returns a pointer to one of its local variables, and since this local variable is defined only from the function, the pointer becomes invalid once the function ends.

Example:
char *dp =NUL                 
{
char c;
dp = &c;
} // c falls out of scope //dp is now a dangling pointer
dp

1001

NULL Pointer in C:
A pointer that is assigned NULL is called a null pointer and this is done at the time of a variable declaration.
Assign a NULL value to a pointer variable, in case we do not have an exact address to be assigned. It is a constant, with a value of zero defined in standard libraries.
Example
#inlcude<stdio.h>
int main()
{
int *pr = NULL;
printf(“The value of pr is: %x\n”, &pr);
return 0;
}

OUTPUT: The value of pr is fff4

Top Post Ad

Below Post Ad