In C++, the term “dynamic memory deployment” refers to the manual allotment of storage by a programmer. Heap is where distributed shared memory is deployed, while Stack is where non-static and local variables acquire memory resources.
Applications
- Allocating storage of dynamic size, which is not achievable with compiler-allocated storage apart from varying arrays, is one usage of dynamic memory allocation.
- The main benefit is the versatility it gives to programmers. We have absolute autonomy to allocate and release memory as needed and as no longer required. This versatility is useful in many situations.
How Does C++ Allocate and Deallocate Memory?
The malloc() and calloc() methods in C++ are used to dynamically change memory at execution. While the free() method is used to release the memory that has been allocated. These methods are provided by C++ together with the two ‘new’ and ‘delete’ operators that make memory allocation and release easier and more efficient.
The Correlation between the ‘New’ Keyword and ‘New’ Operator New
These two things are different from one another.
Two events take place: memory distribution and object formation. Both are handled by the ‘new’ keyword. Calling the operator and calling the constructor itself are the procedures to allocate storage. The operator ‘new’ will not take any responsibility for invoking the constructor but it does let you adjust the storage allocation strategy. The new keyword has that responsibility. Calling the constructor without using operator new is feasible.
New Operator
Demand for memory allocation on the Open Storage is indicated by the new operator. If enough memory is accessible, a new operator processes data. Then, initializes it and gives the pointer object the address of the recently allocated and configured memory.
Example 1
We execute this code to show how the new and delete operators can be utilized; the memory will be dynamically allocated and released.
using namespace std;
int main ()
{
int* a = NULL;
a = new(nothrow) int;
if (!a)
cout << "Memory is not allocated\n";
else
{
*a = 45;
cout << "Value of a: " << *a << endl;
}
float *c = new float(67.84);
cout << "Value of c: " << *c << endl;
int x = 6;
int *b = new(nothrow) int[x];
if (!b)
cout << "Memory is not allocated\n";
else
{
for (int j = 0; j < x; j++)
b[j] = j+1;
cout << "A chunk of memory containing values: ";
for (int j = 0; j < x; j++)
cout << b[j] << " ";
}
delete a;
delete c;
delete[] b;
return 0;
}
The required libraries <iostream>would be integrated. Next, the standard namespace will be added as ‘std’. Here, the main() function would be invoked. We would initialize a pointer and set this pointer to ‘NULL’. Let’s request storing the variables that are utilized for a ‘new’ operator. The data type of ‘a’ will be ‘integer’. The ‘if-else’ condition would apply. If the defined condition is satisfied, the ‘cout’ statement will display the text ‘Memory is not allocated’. Otherwise, we would store the value of the pointer.
Then, the ‘cout’ command shows the value of the pointer. Now, we would request a block of memory by utilizing the ‘new’ operator. We specify the floating-point value of the variable ‘c’ by using the ‘new’ operator. Now, the ‘cout’ will print the value of variable ‘c’. Here, we specify the size of the block of memory. We declare a variable ‘x’ and set its data type as an ‘integer’. We construct a new pointer named ‘b’ and provide the value to it by using the ‘new’ operator.
Once again, the ‘if-else’ condition will be utilized. Within the ‘else’ statement we have used the ‘for’ statement. Initially, the loop variable ‘j’ would be declared and we define the condition. Then, we increase the value of the loop variable by 1. The ‘cout’ command would be used to print the line ‘A chunk of memory containing values’. Once again, we will employ the ‘for’ loop.
Now, we will free the allocated memory by the utilization of the ‘delete’ operator. Before adding the ‘return 0’ command, we free the block of allocated memory by using the array.
We acquire this type of outcome after executing the above-mentioned program.
Example 2
In this instance, we will utilize the ‘new’ operator for different objects.
#include <string>
using namespace std;
int main()
{
int *l = NULL;
l = new int();
int *v = new int(23);
if(!l)
{
cout<<"bad memory allocation"<<endl;
}
else
{
cout<<"memory allocated successfully"<<endl;
*l = 20;
cout<<"*l = "<<*l<<endl;
cout<<"*v = "<<*v<<endl;
}
double *arr = NULL;
arr = new double[20];
if(!arr)
{cout<<"memory not allocated"<<endl;}
else
{
for(int k=0;k<20;k++)
arr[k] = k+1;
cout<<"arr values : ";
for(int k=0;k<20;k++)
cout<< arr[k]<<"\t";
}
delete l;
delete v;
delete[] arr;
return 0;
}
In the illustration is the integration of the necessary libraries <iostream>. Next, the namespace known as “std” will be added. The main() method would be called. To initialize a pointer, we would set it to “NULL.” The value of the variable “i” that is used for the “new” operator would be stored here. This variable’s data type is “integer.” The “if-else” condition will be utilized. If the specified condition is met, the text “Bad memory allocation” will be displayed by the “cout” statement. If not, we would keep the pointer’s value.
Then, the text “memory allocated successfully” is displayed using the “cout” command. The pointer’s value would be set. The value of the pointer would be displayed by the ‘cout’ command. We initialized the array with the data type “double” and gave it the value “NULL.” Using the ‘new’ operator, we define the value of this array. It uses an “if-else” statement. When the condition is met, the “cout” command prints “memory not allocated.”; else, we have used nested “for” statements.
Before defining the condition, the loop variable “j” would first be specified. Next, we add 1 to the value of ‘j’. The array’s values would be printed using the ‘cout’ command. We would use the “for” loop once again. Now, we will be using the delete operator. We will release the memory that was allocated. We release the block of allocated memory by utilizing the array before adding the “return 0” statement.
After implementing the aforementioned code, we get the desired result.
Conclusion
The usage of the ‘new’ operator is the focus of this article. For user-defined data types in classes and other data types, we will utilize the ‘new’ operator. We have executed two examples related to this topic. The first example demonstrates how to use C++’s ‘new’ operator. The last illustration shows how to apply the “new” operator to objects.