What is dangling pointers?and what is memory leak?
Answer Posted / rishabh agrawal
A1 - Dangling pointer points to a memory location which is
being removed.
E.g.
int* a = new int;
int *b = a;
delete b;
Now a will be a dandling pointer.
A2 - Memory leaks occur when memory allocated to a pointer
is not deleted and the pointer goes out of scope. This
allocated memory never gets de-allocated and remains in the
heap until the system is restarted.
E.g.
void foo()
{
int* ptr = new int;
*ptr = 10;
...
...
}
Since ptr is allocated memory using 'new', but no 'delete'
is called, hence memory allocated to ptr will leak.
Is This Answer Correct ? | 106 Yes | 22 No |
Post New Answer View All Answers
What is static function? Explain with an example
What are the benefits of operator overloading?
Can we use this pointer inside static member function?
Write a program to find the reverse Fibonacci series starting from N.
Explain the benefits of proper inheritance.
What is private inheritance?
Can a constructor return a value?
What is problem with overriding functions?
What is a node class in c++?
Is it possible to write a c++ template to check for a function's existence?
How do you initialize a string in c++?
What can c++ be used for?
What is isdigit c++?
Who calls main function?
What is the identity function in c++? How is it useful?