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 the main purpose of inheritance law?
What is the point of oop?
Can we have inheritance without polymorphism?
What is polymorphism used for?
Is react oop?
class type to basic type conversion
Why is it so that we can have virtual constructors but we cannot have virtual destructors?
What is destructor in oop?
What is encapsulation and abstraction? How are they implemented in C++?
How do you achieve runtime polymorphism?
What are the 5 oop principles?
Please send ford technologies placement paper 2 my mail id
i got a backdoor offer in process global,Bangalore..Can i work with it?
What is the purpose of enum?
Why is oop better than procedural?