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

WHAT IS THE ACTUAL DEFINATION OF OBJECT AND THE CLASS IN ONE SINGLE LINE WHICH THE INTERVIEWER WANT TO LISTEN.

11 Answers  


You attempt to query the data base with this command: SELECT name, salary FROM employee WHERE salary=(SELECT salary FROM employee WHERE last name='Wagner' OR dept no=233) Choose most appropriate option from the following: 1)Sub-queries are not allowed in the where clause. 2)a multiple row sub-query used with a single row comparison operator. 3)a single row query is used with a multiple row comparison operator.

10 Answers   Zycus Infotech,


write a code for this. serial_number contained in the header of the file will be read , if this serial number is less than a previous serial number within a successfully processed file, or is the same as another serial number within a successfully processed file, or if the field contains anything other than 7 digits, then the file must error with the reason ‘Invalid SERIAL_NUMBER’.

0 Answers   Microsoft, TCS,


how do u initialize the constant variables

5 Answers   IBM, Siemens,


What is destructor example?

0 Answers  






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

2 Answers  


create a c++ program that will ask 10 numbers and display their sum using array.

1 Answers  


What is ambiguity in c++

4 Answers   Accenture, Mphasis,


What is overriding vs overloading?

0 Answers  


What is variable example?

0 Answers  


What is the difference between pass by reference and pass by value?

12 Answers   Pfizer, TCS,


What are the fields of vtable

1 Answers   Mphasis,


Categories