1.

What Is Dangling Pointer In C?

Answer»

If any POINTER is pointing at the memory location/address of any variable, but if the variable is deleted or does not EXIST in the current scope of code, while pointer is STILL pointing to that memory location, then the pointer is called dangling pointer. For example,
#include<stdio.h>
INT *func();
int MAIN()
{
int *ptr;
ptr=func();
printf("%d",*ptr);
return 0;
}
int * func()
{
int x=18;
return &x;
}
Output is Garbage value, since the variable x has been freed as soon as the function func() returned

If any pointer is pointing at the memory location/address of any variable, but if the variable is deleted or does not exist in the current scope of code, while pointer is still pointing to that memory location, then the pointer is called dangling pointer. For example,
#include<stdio.h>
int *func();
int main()
{
int *ptr;
ptr=func();
printf("%d",*ptr);
return 0;
}
int * func()
{
int x=18;
return &x;
}
Output is Garbage value, since the variable x has been freed as soon as the function func() returned



Discussion

No Comment Found