What is memory leaking in c++ ?

Showing Answers 1 - 4 of 4 Answers

KAMLESH V.CHOPDE

  • Jan 20th, 2006
 

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. 

  Was this answer useful?  Yes

akif

  • May 3rd, 2006
 

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

bappa

  • Jun 5th, 2006
 

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

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

  Was this answer useful?  Yes

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).

ajrobb

  • Sep 22nd, 2010
 

A memory leak in C++ is generally caused by not understanding the language.

The std::auto_ptr<class> or similar should be used whenever allocating objects on the heap using new. This is essential in constructors, as an exception will call the destructor on all objects already allocated but an ordinary pointer is a basic type and there is no destructor. Arrays should be allocated through std::vector<class> rather than new[].

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions