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.
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.
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.
In the above example, 'p' is a pointer of type int.
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:
Assigning a value to the newly created object
Two ways of assigning values to the newly created object:
We assign 45 to the newly created int object and 9.8 to the newly created float object.
Let's look at some examples.
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:
In the above statement, we have created an array of type int having a size equal to 8 where p refers first element, p refers the first element, and so on.
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:
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:
The dynamically allocated array can also be removed from the memory space by using the following syntax:
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:
Let's understand through a simple example:
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.
Advantages of the new operator
The following are the advantages of the new operator over malloc() function: