answersLogoWhite

0

Memory leakage occurs when you allocate memory dynamically but do not keep track of the allocation. If you lose track of an allocation then you cannot release the memory back to the system until the program terminates. If you continually allocate without releasing, you not only waste a lot of memory unnecessarily, you could easily run out of memory altogether. The following code demonstrates this:

#include <iostream>

int main()

{

int* p=NULL;

do {

p=(int*)malloc(sizeof(int));

} while(p);

return(0);

}

In the above code, p is continually allocated memory which is not released before the next allocation. The program will eventually exit when you run out of memory, releasing all memory back to the system, but you should never code like this. Always release memory as soon as it is no longer required.

#include <iostream>

int main()

{

int* p=(int*)malloc(sizeof(int));

while(p)

{

delete(p); // release memory

p=(int*)malloc(sizeof(int));

}

return(0);

}

This time the program will never terminate so long as p is non-NULL. Although the program may never exit, memory is no longer leaking.

Obviously these programs are merely demonstrations and not useful in any way (we're not actually using the memory we allocate). In real applications, however, you will always delete pointers as soon as you're finished with them and especially when you wish to re-use a pointer, as per the second example.

So long as you maintain at least one pointer to allocated memory you can return that memory back to the system as soon as it is no longer required. If you fail to keep track of allocated memory, leakage is only to be expected. Even if it doesn't cause any problems, as per the first example, it is bad programming practice to allow any pointer to allocated memory to fall from scope.

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

What is the memory management operator in c plus plus?

There is no memory management operator in C++ -- it is an unmanaged language. You use the C++ new operator to allocate memory, and use the C++ delete operator to release previously allocated memory.


Which operator is used for deallocating memory in c plus plus?

delete


What are new and delete operators in c plus plus?

New and Delete are the memory management operators in c++,like c language we use malloc() and calloc() functions to allocate memory and free() functiong to release the memory similarily we use new to allocate memory in C++ and Delete to release the allocated memory....


What is an address in C plus plus programming?

An address in C or C++ is the location in memory of an object or function. An address is the contents of a pointer, as opposed to the contents of the memory location pointed to by the pointer.


How do you calculate the size of a class in memory in C plus plus?

Use sizeof( ).


What is memory leaking in c plus plus?

Memory leaking in C++, or in any language that supports dynamic memory allocation, is a failure to release memory when its use is no longer required. This causes the memory image of the process to grow, sometimes without bounds, ultimately causing process failure due to memory exhaustion.


What is a distructor in c plus plus?

A destructor destroys an instance of a class to free up memory.


What are the storage allocation in C plus plus?

They mostly deal with pointers and new operators in memory.


What is the operator is used to allocate the memory in c plus plus?

calloc operator,malloc operator


What is a scatter file in c or c double plus programming?

helps you to organise the memory as you want


How many classes can we write in a single c plus plus program?

Its limited only by available memory.


What is memory leakage in C?

When you are using pointers and you don't free memory after you stoped using it, you loose certain amount of memory, which you cannot use and system cannot use either because it was reserved. When you work with huge amounts of memory like arrays you can run out of memory which will cause a fatal error.