What is dangling pointers?and what is memory leak?
Answer Posted / kush
a dangling pointer is a pointer which points to a dead location in memory, means the variable or object is exist on that memory location bt that was deleted and pointer is still pointing to that location . this pointer is called dangling pointer.
EX:
int *function()
{
int a=500;
return &a;
}
int main()
{
int *ptr;
ptr=function();
++*ptr;
printf("%d",*ptr);//output=501
}
in the above example one function is there which is having return type int * means this function retuns a address.
now came int main(), i have creating one pointer *ptr and after that calling the function then whatever the address return by function is stored in *ptr.
but see the storage class of variable a in function it is auto by default and when function execution is end that variable is destroyed and funtion will return the address of variable a.
tha variable is destroy bt my *ptr is still pointing and utilizing that meroy location and data on that location this is called dangling pointer..
thanks...
| Is This Answer Correct ? | 28 Yes | 5 No |
Post New Answer View All Answers
what is a class? Explain with an example.
What are c++ files?
Which bit wise operator is suitable for turning off a particular bit in a number?
What is basic if statement syntax?
Explain what are single and multiple inheritances in c++?
Eplain extern keyword?
Why do we use structure in c++?
Is c# written in c++?
What is a block in c++?
What is the best c c++ compiler for windows?
Do we have to use initialization list in spite of the assignment in constructors?
Is arr and &arr are same expression for an array?
Which programming language's unsatisfactory performance led to the discovery of c++?
How much maximum can you allocate in a single call to malloc()?
What is object in c++ wikipedia?