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
How is c++ different from java?
How to allocate memory dynamically for a reference?
Can I have a reference as a data member of a class? If yes, then how do I initialise it?
What are the two types of comments?
Explain this pointer?
There are 100 students in a class. The management keep information in two tables. Those two tables are given like Roll no Name Age 001 ABC 15 002 XYZ 14 and Roll No Subject Marks 001 Math 75 001 Physics 55 002 Math 68 001 Hindi 69 They want the information like this Roll No Name Hindi Physics Math Total 001 ABC 69 55 75 199 002 XYZ 68 74 84 226 And Roll No Suject Highest 001 Math 98 007 Physics 84 021 Hindi 74 All 275 All information is kept in structure in main memory. You have to find last two tables.
What is buffer and example?
What is ifstream c++?
How does code-bloating occur in c++?
What is insertion sorting?
What is conditions when using boolean operators?
What is enum c++?
What's the hardest coding language?
Is overriding possible in c++?
What is a catch statement?