GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 62 of 203    Print  
what is memory leaking in c++ ?

  
Total Answers and Comments: 4 Last Update: June 13, 2007     Asked by: gopala krishna 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: SomGollakota
 
Memory leak is - dynamically allocating memory and forgeting to free it. In C++, using a new operator to allocate a chunk of memory, and forgetting to delete it. There are several reasons this could occur. Some of them are,
1. Allocate a chunk of memory and assign the starting address to a pointer variable, and later, reassing another address to the same pointer without freeing its original address
2. Allocate memory for an array and not using proper delete method (delete[]) to free the memory
3. Unhandled exceptions - allocating memory and deleting it at the end of the program, but the program abnormally terminates (crashes) before the delete code line is executed.

Some of the ways of avoiding memory leaks within a class object are
1. Allocate all dynamic memory required for the class in the constructor, and delete it in the distructor
2. Use TRY/CATCH blocks around all statements where there is a possiblity of a crash
3. Override new operator for your class and Track memory allocations and pointers. Override delete operator and untrack the deleted/freed memory. In the destructor, check to see if there are any tracked memory locations (not freed) and free them up.

But beyond all these, it's still a good programming style that avoids memory leaks, because beyond objects, there is a program where you instantiate and run these objects. If you are allocating memory dynamically in the parent program, then carefully make sure you delete those chunks at all exit points (or centralize your exit point).

Above answer was rated as good by the following members:
wael.salman
January 20, 2006 16:58:35   #1  
KAMLESH V.CHOPDE        

RE: what is memory leaking in c++ ?

When a class uses dynamically allocated memory internally all sorts of problems arises.If not properly used or handled they can lead to memory leaks & corrupts Data Structures.

Amemory leak is the situation that occurs when dynamically allocated memory is lost to the program.

char * p;

p new char[10000];

.....

p new char[5000];

Initially 10000 bytes are dynamicallyallocated & the the address of those bytes is stored in p. later 5000 bytes are dyanmically allocated & the address is stored in p. However the original 10000 bytes 've not been returned to the system using delete [] opertor .

Memory leak actually depends on the nature of the program.


 
Is this answer useful? Yes | No
May 03, 2006 05:54:22   #2  
akif        

RE: what is memory leaking in c++ ?

hiiiiiiiiii

memory leaking means u store some data in memory but u forget the address of the block E.g

int *i new int[2000]

delete i

u delete the address of the memory not hole memory

block


 
Is this answer useful? Yes | No
June 05, 2006 06:13:05   #3  
bappa        

RE: what is memory leaking in c++ ?

Memory leak means u use some dynamic memory but forget to free them....

use calloc() insted of malloc().....


 
Is this answer useful? Yes | No
June 13, 2007 14:47:31   #4  
SomGollakota Member Since: June 2007   Contribution: 48    

RE: what is memory leaking in c++ ?
Memory leak is - dynamically allocating memory and forgeting to free it. In C++ using a new operator to allocate a chunk of memory and forgetting to delete it. There are several reasons this could occur. Some of them are
1. Allocate a chunk of memory and assign the starting address to a pointer variable and later reassing another address to the same pointer without freeing its original address
2. Allocate memory for an array and not using proper delete method (delete[]) to free the memory
3. Unhandled exceptions - allocating memory and deleting it at the end of the program but the program abnormally terminates (crashes) before the delete code line is executed.

Some of the ways of avoiding memory leaks within a class object are
1. Allocate all dynamic memory required for the class in the constructor and delete it in the distructor
2. Use TRY/CATCH blocks around all statements where there is a possiblity of a crash
3. Override new operator for your class and Track memory allocations and pointers. Override delete operator and untrack the deleted/freed memory. In the destructor check to see if there are any tracked memory locations (not freed) and free them up.

But beyond all these it's still a good programming style that avoids memory leaks because beyond objects there is a program where you instantiate and run these objects. If you are allocating memory dynamically in the parent program then carefully make sure you delete those chunks at all exit points (or centralize your exit point).

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape