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


Please Help Members By Posting Answers For Below Questions

What is static function? Explain with an example

675


What are the benefits of operator overloading?

787


Can we use this pointer inside static member function?

717


Write a program to find the reverse Fibonacci series starting from N.

691


Explain the benefits of proper inheritance.

728






What is private inheritance?

680


Can a constructor return a value?

676


What is problem with overriding functions?

676


What is a node class in c++?

733


Is it possible to write a c++ template to check for a function's existence?

664


How do you initialize a string in c++?

651


What can c++ be used for?

678


What is isdigit c++?

684


Who calls main function?

676


What is the identity function in c++? How is it useful?

650