What is memory leak and memory corruption?



What is memory leak and memory corruption?..

Answer / 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

More OOPS Interview Questions

DIFFRENCE BETWEEN STRUCTURED PROGRAMING AND OBJCET ORIENTED PROGRAMING.

5 Answers  


who is the father of OPPS

4 Answers   Infosys, TCS,


What is inheritance write a program to show use of inheritance?

0 Answers  


explain defference between structure and class with example

1 Answers  


What is encapsulation selenium?

0 Answers  


What is destructor example?

0 Answers  


There are 2 classes defined as below public class A { class B b; } public class B { class A a; } compiler gives error. How to fix it?

3 Answers   Microsoft,


what is the difference between class to class type conversion and copy constructor ?

2 Answers  


C#.net Interview Question A=10 B=5 C=A+B Print C The above will be given in a multiline textbox. You need to parse the above input, store values for A,B&c. And you have to display the value of C.

1 Answers   Syncfusion,


write a c++ code of diagonal matrix.

2 Answers  


Write A Program using Single and Multiple Inheritance.

1 Answers  


which feature are not hold visual basic of oop?

0 Answers   Ignou,


Categories