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  


What are the different forms of polymorphism??

8 Answers   Mantaq, NUC, PCS,


how to get the oracle certification? send me the answer

0 Answers   Oracle,


What is the difference between class and object?

4 Answers   Apple, Heinz,


swapping program does not use third variable

5 Answers   TCS,






This program numbers the lines found in a text file. Write a program that reads text from a file and outputs each line preceded by a line number. Print the line number right-adjusted in a field of 3 spaces. Follow the line number with a colon, then one space, then the text of the line. You should get a character at a time and write code to ignore leading blanks on each line. You may assume that the lines are short enough to fit within a line on the screen. Otherwise, allow default printer or screen output behavior if the line is too long (i.e., wrap or truncate). A somewhat harder version determines the number of spaces needed in the field for the line numbers by counting lines before processing the lines of the file. This version of the program should insert a new line after the last complete word that will fit within a 72-character line.

0 Answers  


What is advantage of inheritance?

0 Answers  


Child cObj = new Parent() Wahts the output ?

8 Answers   Patni, TCS,


Write a program to find out the number of palindromes in a sentence.

1 Answers   TCS,


Is oop better than procedural?

0 Answers  


What is encapsulation oop?

0 Answers  


for example A,B,C,D are class all the 4 class contain one method who() but the method who() implementaion is differnet among each class. the relation among the 4 class are A is base class and is inherited by B and C.and from this two B and C where D is inherited. the question is i want to display the output who() method in all the classes(A,B,C,D)the output of who() method is diferrent amond all the class(A,B,C,D) ------A------ virtuval who(print a) override | | who(print b) B C override who(print c) | | -------D------ override who(print d)

2 Answers  


Categories