12. Memory management

By | September 22, 2021

What is Memory Management?

Memory management is a process of managing computer memory, assigning the memory space to the programs to improve the overall system performance.

Why is memory management required?

As we know that arrays store homogeneous data, so most of the time, memory is allocated to the array at the declaration time. Sometimes the situation arises when the exact memory is not determined until runtime. To avoid such a situation, we declare an array with a maximum size, but some memory will be unused. To avoid the wastage of memory, we use the new operator to allocate the memory dynamically at the run time.

Dynamic Memory

A good understanding of how dynamic memory really works in C++ is essential to becoming a good C++ programmer. Memory in your C++ program is divided into two parts −

  • The stack − All variables declared inside the function will take up memory from the stack.
  • The heap − This is unused memory of the program and can be used to allocate the memory dynamically when program runs.

Many times, you are not aware in advance how much memory you will need to store particular information in a defined variable and the size of required memory can be determined at run time.

You can allocate memory at run time within the heap for the variable of a given type using a special operator in C++ which returns the address of the space allocated. This operator is called new operator.

If you are not in need of dynamically allocated memory anymore, you can use delete operator, which de-allocates memory that was previously allocated by new operator.

new and delete Operators

01. New operator

new operator is used to create the object while a delete operator is used to delete the object. When the object is created by using the new operator, then the object will exist until we explicitly use the delete operator to delete the object. Therefore, we can say that the lifetime of the object is not related to the block structure of the program.

There is following generic syntax to use new operator to allocate memory dynamically for any data-type.

Syntax

pointer_variable = new data-type ;

The above syntax is used to create the object using the new operator. In the above syntax, ‘pointer_variable’ is the name of the pointer variable, ‘new’ is the operator, and ‘data-type’ defines the type of the data.

Example 1:

int *p;
p = new int;  

In the above example, ‘p’ is a pointer of type int.

Example 2:

float *q;   
q = new float;  

In the above example, ‘q’ is a pointer of type float.

In the above case, the declaration of pointers and their assignments are done separately. We can also combine these two statements as follows:

int *p = new int
float *q =   new float;   

Assigning a value to the newly created object

Two ways of assigning values to the newly created object:

We can assign the value to the newly created object by simply using the assignment operator. In the above case, we have created two pointers ‘p’ and ‘q’ of type int and float, respectively. Now, we assign the values as follows:

*p = 45;  
*q = 9.8;  

We assign 45 to the newly created int object and 9.8 to the newly created float object.

  • We can also assign the values by using new operator which can be done as follows:

pointer_variable = new data-type(value);  

Let’s look at some examples.

int *p = new int(45);  
float *p = new float(9.8);  

How to create a single dimensional array

As we know that new operator is used to create memory space for any data-type or even user-defined data type such as an array, structures, unions, etc., so the syntax for creating a one-dimensional array is given below:

pointer-variable = new data-type[size];  

Examples:

int *a1 = new int[8];  

In the above statement, we have created an array of type int having a size equal to 8 where p[0] refers first element, p[1] refers the first element, and so on.

02. Delete operator

When memory is no longer required, then it needs to be deallocated so that the memory can be used for another purpose. This can be achieved by using the delete operator, as shown below:

delete pointer_variable;   

In the above statement, ‘delete’ is the operator used to delete the existing object, and ‘pointer_variable’ is the name of the pointer variable.

In the previous case, we have created two pointers ‘p’ and ‘q’ by using the new operator, and can be deleted by using the following statements:

delete p; 
delete q;  

The dynamically allocated array can also be removed from the memory space by using the following syntax:

delete [size] pointer_variable;   

In the above statement, we need to specify the size that defines the number of elements that are required to be freed. The drawback of this syntax is that we need to remember the size of the array. But, in recent versions of C++, we do not need to mention the size as follows:

delete [ ] pointer_variable;   

Let’s understand through a simple example:

#include <iostream>  
using namespace std  
int main()  
{  
int size;  // variable declaration  
int *arr = new int[size];   // creating an array   
cout<<“Enter the size of the array : “;     
std::cin >> size;    //   
cout<<“\nEnter the element : “;  
for(int i=0;i<size;i++)   // for loop  
{  
cin>>arr[i];  
}  
cout<<“\nThe elements that you have entered are :”;  
for(int i=0;i<size;i++)    // for loop  
{  
cout<<arr[i]<<“,”;  
}  
delete arr;  // deleting an existing array.  
return 0;  
}  

In the above code, we have created an array using the new operator. The above program will take the user input for the size of an array at the run time. When the program completes all the operations, then it deletes the object by using the statement delete arr.

Output:

C++ Memory Management

Advantages of the new operator

The following are the advantages of the new operator over malloc() function:

  • It does not use the sizeof() operator as it automatically computes the size of the data object.
  • It automatically returns the correct data type pointer, so it does not need to use the typecasting.
  • Like other operators, the new and delete operator can also be overloaded.
  • It also allows you to initialize the data object while creating the memory space for the object.

Leave a Reply

Your email address will not be published. Required fields are marked *