Memory Management

The new Operator

In order to request dynamic memory there is the operator new. This operator new is followed by a data type and optionally the number of elements required within brackets[]. IT returns a pointer to the beginning of the block assigned memory. Its form is:

pointer=new type

or

pointer=new type[Number of elements]

The first expression is used to assign memory that will hold one single element of a data type. The second one is used to assign a block (an array) of elements of a data type.

For example:

int *bobby;

bobby=new int[5];

In this case, the operating system has assigned space for a 5 elements of the type int in the heap and it has returned a pointer that has been assigned to the variable bobby. therefore, now bobby points to the beginning of a valid block of memory with space of 5 int elements.

What is the difference between declaring a normal array and assigning memory to a pointer?

The most important one is that the size of an array must be a constant value. This limits its size to what we decide at the moment of designing the program before its execution. However, dynamic memory allocation allows us to assign memory during the execution of the program using any variable, constant or a combination f both as size.

The delete Operator

Since dynamic memory is needed only for certain moments within a program, once it is no longer needed, it should be freed so that it become availble for future requests. For this purpose the operaor delete can be used, whose form is:

delete pointer;

or

delete []pointer'

The first expression should be used to delete memory alloctated for a single element, and the second one for momory allocated for multiple elements (arrays).

If your program reserves many chunks of memory using new, eventually all the variable memory will be reserved and the system will crash. To ensure safe and efficient use of memory, the use of the new operator is followed by corresponding delete operator that returns memory to the operating system. The statement

delete ptr;

returnd the memory pointed to by ptr to the operating system.

Free Storage Pool

The operators new and delete are maintained by the memory management system. Whenever a request is made by a program, the system either gives or takes back some memory depending upon the request. The pool of unallocated heap memory that is used for dynamic memory allocation with the help of the new operator is known as free storage pool.

Memory Leaks

The new operator is used for dynamic memory allocation. If the memory allocted by the new operaotr is not deleted using delete operator, it will leave an abandoned memory block.

If a function allocates memory dynamically but forgets to delete it later, it occupies some amount of memory every time it is executed. Thus, a part of the memory is being occupied in every run. Finally all the memory area will be filled. This situation is known as memory leak.

Reasons for Memory Leak

1. Forgetting to sue delete operator

2. The delete operator is in an unreachable code area of the program.

3. Using the new operator for allocating memory with a pointer that is already pointing to an

allocated object.

HOME LEARN C++ PREVIOUS NEXT