C++ Memory Management

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 the 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.

Memory Management Operators

In C language, we use the malloc() or calloc() functions to allocate the memory dynamically at run time, and free() function is used to deallocate the dynamically allocated memory. C++ also supports these functions, but C++ also defines unary operators such as new and delete to perform the same tasks, i.e., allocating and freeing the memory.

New operator

A 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.

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
snippet
int *p;
p = new int;

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

Example #2
snippet
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:

snippet
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:

Using Assignment Operator

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.

Example
snippet
*p = 45;
*q = 9.8;

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

Using new operator

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

Syntax
snippet
pointer_variable = new data-type(value);
Example

Let's look at some examples.

snippet
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:

Syntax
snippet
pointer-variable = new data-type[size];
Examples
snippet
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.

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:

Syntax
snippet
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:

Example
snippet
delete p;
delete q;

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

Syntax#1
snippet
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:

Syntax#2
snippet
delete [ ] pointer_variable;
Example

Let's understand through a simple example:

snippet
#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.
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +