What is Memory Management In C++?

To manage the computer memory and space to improve memory and system performance we need Memory Management In C++.

Why is memory management required?

Generally, everybody knows the array stores homogenous data and the size are fixed and can’t change during runtime.

So, the array allocates the memory at compilation time.

There is some situation that arises where it is not determined the size until the run time.

To overcome this situation we allocate the maximum size and in that case unused memory space increases.

To save memory space from wastage we use a “new” operator to allocate memory at runtime i.e. called dynamic memory allocation.

Memory Management Operators:

In the case of C programming, there are two important functions are available to handle dynamic memory allocation i.e. malloc() and calloc().

Both functions are belongs to “<stadlib.h>” header file.

And after allocation, there is a need to deallocate for the purpose of allocating new objects we use free().

Inside C++ supports these two functions but here also defines unary operators such as new and delete for the purpose of same use like malloc() and free().

New Operator:

A new operator works for creating the object while a delete operator is used to deleting the object.

There are some questions that arise in your mind like Why we need to delete the operator?

The answer is when we allocate the memory dynamically it does not release the memory after the execution of code. So we need a delete operator to helps us remove the object.

Syntax:

pointer_variable = new data-type  

This is the syntax of creating the object with help of a new operator.

The syntax ‘pointer_variable‘ is the name of the pointer variable, ‘new’ is the operator, and the ‘data-type’ type of data defines the object.

Example 1:

int *q = new int;  

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

Example 2:

double *p = new double;  

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

Don’t use multiple line for object creation on dynamic like,

int *p = new int;

Let’s see and complete an Example:

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
    int *q = new int;

  	*q = 34;
  
  	cout<<"q: "<<*q; 

    free(q);

    return 0;
}
Output

q = 34

Let’s see another Example:

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
    float *q = new float;

  	*q = 34.7564;
  
  	cout<<"q: "<<*q; 

    free(q);

    return 0;
}
Output:

q: 34.7564

How To Assigning A Value To The Newly Created Object In C++:

There are two ways to assigning value to the newly created object i.e:

  1. By help of assignment operator we can assign data to the newly created object.
*p = 23.5757;
*q = 65;

In the above case, we already created two objects and assigning values to them.

Here 23.5757 is to assign a newly created float object and 65 is to assign a newly created int object.

2. And we can assign values while the object initialization time like,

pointer_variable = new data-type(data);

Let’s see the below example:

int *p = new int(34);
float *q = new float(23.45);

Now, How To Declare One Dimentional Array? :

As we already know, how new operator helps us to create the object of any type, like int, float, double, long, char.

Likewise, it also helps us to declare the object like Array, structures, unions, etc.

So now is the time to declare a one-dimensional array is given below:

pointer_variable = new data-type[size];

Now the Example:

int *z = new int[5];

In the above statement, here we have created an array of object types i.e. *z and its size is 5.

So, here we accessing the elements like normal array elements accessing, z[0] access the first element, z[1] access the second element.

Delete Operator:

When the work of that particular object is completed then we need to release the space for other objects and also a better use of memory we need to delete the object with the help of the delete operator.

It removes the object and clears the space.

delete pointer_variable;

In the above statement, delete is the operator and pointer_variable is the name of the pointer object.

In the previous example, we have created two-pointer objects 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 help of the following syntax:

delete [size] pointer_variable;

In the above statement, we need to specify the size of the pointer object which is dynamically allocated and required for free.

The drawback of this syntax is that we need to remember the size of the array. But, now some recent versions of C++, we do not need to mention the size as follows:

delete [ ] pointer_variable;

Let’s see an example:

//Dynamically Allocate Array Using "new" And "delete" operator

#include <iostream>

using namespace std;

int main()
{
    int *p = new int[5];
    cout << "Enter the elements in an Array:\n";
    for (int i = 0; i < 5; i++)
    {
        cin >> p[i];
    }

    cout << "display elements in an array:\n";
    for (int i = 0; i < 5; i++)
    {
        cout << p[i] << "," << endl; //p[0], p[1], p[2]
    }

    delete p;

    cout << "display elements in an array: after delete operator: \n";
    for (int i = 0; i < 5; i++)
    {
        cout << p[i] << ",";
    }

    return 0;
}

Output:

Enter the elements in an Array:
1
2
3
4
5
display elements in an array:
1,
2,
3,
4,
5,
display elements in an array: after delete operator:
17595312,17563840,3,4,5, //Returns Garbage Value

Output

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

In the above program, you have created an array using a new operator.

This program takes user input for the size of an array at the runtime.

When the program completes its task, then it deletes the object p by using a statement delete p.

Advantages of the “new” operator:

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

  • It doesn’t use the sizeof() operator to return the size of the data object.
  • It doesn’t need to use typecasting for a number of data objects are used.
  • Like other operators, new and delete operators can also be overloaded.
  • It generally initializes the data object at the time of declaration while creating the object.

If you like the article please comment and share this article with your friends and wants to join our WhatsApp group.

Default image
Lingaraj Senapati
Hey There! I am Lingaraj Senapati, the Co-founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.
Articles: 228

Newsletter Updates

Enter your email address below to subscribe to our newsletter