What is memory leak and memory corruption?

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


Please Help Members By Posting Answers For Below Questions

What is an example of genetic polymorphism?

645


What is coupling in oops?

589


Why is oop better than procedural?

600


What is encapsulation with example?

571


What polymorphism means?

615






Are polymorphisms mutations?

691


What is encapsulation in simple terms?

534


What are the types of abstraction?

551


What is encapsulation in oop?

599


Can we create object of abstract class?

573


What is the use of oops?

614


What is a class and object?

589


How to hide the base class functionality in Inheritance?

630


What does it mean when someone says I oop?

574


Where is pseudocode used?

560