Answer Posted / shyamal bose
Memory leaks happens for the memory allocated on Heap(ex A
*temp = new A()) . memory allocated by us on stack (int a)
is released automatically when the function returns or
module goes out of scope.
But memory allocated on heap will not be freed
automatically, we need to release it manually.
ex:
func()
{
A *a = new A(); //on heap
int b; // on stack
}
main()
{
func();
}
Now in above example when func is called memory for "a" is
created on HEAP by using NEW, but it is not freed by using
DELETE, hence is memory leak. On the other hand "b" is
created on STACK & freed automatically. so correct
implementation is:
func()
{
A *a = new A(); //on heap
int b; // on stack
delete a; //deleting memory on heap
}
main()
{
func();
}
| Is This Answer Correct ? | 7 Yes | 0 No |
Post New Answer View All Answers
What is destructor example?
How to use CMutex, CSemaphore in VC++ MFC
What is encapsulation process?
What are functions in oop?
Why is destructor used?
What is inheritance in simple words?
What is meant by multiple inheritance?
What is the point of polymorphism?
What is new keyword in oops?
Why we use classes in oop?
What is the fundamental idea of oop?
can we make game by using c
What are the data types in oop?
What is data binding in oops?
what is graphics