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 the use of oops?

694


What is polymorphism and its types?

691


What is oops?what is its use in software engineering?

631


What is polymorphism in oops?

637


class type to basic type conversion

1941






What are the types of abstraction?

643


I have One image (means a group photo ) how to split the faces only from the image?............ please send the answer nagadurgaraju@gmail.com thanks in advace...

1717


What is Difeerence between List obj=new ArrayList(); and ArrayList obj=new ArrayList()?

2193


What is difference between data abstraction and encapsulation?

709


What is abstraction in oop with example?

742


officer say me - i am offered to a smoking , then what can you say

1683


What are the 3 principles of oop?

714


What is encapsulation with example?

673


What does and I oop and sksksk mean?

744


what is difference between class template and template class?

2245