Memory Management In C++

Share Your Love

Memory management is a method of controlling computer memory and allocating memory space to programs in order to improve overall system performance.

Why is memory management required?

Because arrays store homogeneous data, most of the time, memory is allocated to the array when it is created. When the precise memory is not determined until runtime, a problem can develop. We declare an array with maximum size to avoid this scenario, but some memory will be left unused. To reduce memory waste, we use the “new” operator to dynamically allocate memory during runtime.

Memory Management Operators:

The malloc() or calloc() functions in C are used to dynamically allocate memory at runtime, and the free() function is used to deallocate the dynamically created memory. These functions are likewise supported in C++, however, unary operators such as new and delete are defined to perform the same job, primarily allocating and freeing memory.

new Operator:

The object is created using the new operator, and it is deleted with the delete operator. When we use the new operator to create an object, the object will exist until we expressly delete it with the delete operator. As a result, we can conclude that the object’s lifetime is unrelated to the program’s block structure.

Syntax Of “new” Operator:

pointer_variable = new data-type  

The new operator is used to create the object in the above syntax. The name of the pointer variable is ‘pointer variable,’ the operator is ‘new,’ and the data type is ‘data-type’ in the preceding syntax.


int *p;
p = new int;  

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


float *q;
q = new float;

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

The declaration of pointers and their assignments are done individually in the example above. These two statements can alternatively be combined as follows:

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

Assigning a value to the newly created object:

There are two ways to provide the newly generated object values:

  • Using the assignment operator, we can assign a value to the newly generated object. In the example above, we’ve constructed two int and float pointers, ‘p’ and ‘q,’ respectively. The values are now assigned as follows:
*p = 45;
*q = 9.8;  

The freshly formed int object gets 45, while the newly produced float object gets 9.8.

  • We can also use the new operator to allocate the values, as shown below:
pointer_variable = new data-type(value);  

Let’s look at some examples.

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

How do you make a one-dimensional array?

Because we know that the new operator may be used to create memory space for any data type, including user-defined data types like arrays, structures, and unions, the syntax for generating a one-dimensional array is as follows:

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


int *a1 = new int[8];

We have generated an array of type int with a size of 8 in the above expression, where p[0] refers to the first element, p[1] refers to the second element, and so on.

delete Operator:

When memory is no longer needed, it needs to be deallocated so that it can be used for anything else. The delete operator can be used to accomplish this, as seen below:

delete pointer_variable;   

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

In the preceding scenario, we used the new operator to generate two pointers, ‘p’ and ‘q,’ which may be erased with the following statements:

delete p;
delete q;  

Using the following syntax, the dynamically allocated array can be removed from the memory space:

delete [size] pointer_variable;

In the preceding sentence, we must supply the size, which specifies the number of elements that must be liberated. The disadvantage of this approach is that we must remember the array’s size. However, we no longer need to mention the size as follows in newer versions of C++:

delete [ ] pointer_variable; 

Let’s look at an example to help you understand:

#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
  cout<<"\nThe elements that you have entered are :";
  for(int i=0;i<size;i++)    // for loop
  delete arr;  // deleting an existing array.
  return 0;


Enter the size of the array :
Enter the element : 1
The elements that you have entered are :1,2,3,4,5,

Using the new operator, we built an array in the code above. At runtime, the above program will accept user input for the size of an array. When the program has completed all of the operations, it uses the delete arr statement to delete the object.

Advantages of the new operator:

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

  • It does not utilize the sizeof() operator because the size of the data object is calculated automatically.
  • It does not necessary to employ typecasting because it returns the correct data type pointer.
  • The new and delete operators, like other operators, can be overloaded.
    It also allows you to initialise the data object while it is being created in memory.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 355

Newsletter Updates

Enter your email address below to subscribe to our newsletter