The new Operator in C++: Dynamic Memory Allocation

Filed Under: C++
C New Operator

Hey, all. In our series of C++ programming, in this article, we talk about the new operator in C++.

So, let us begin!


Working of new operator in C++

The new operator allows us to request the allocation of memory space on a Heap structure. By using the C++ new operator, we can request additional memory during runtime i.e. at dynamic point of time during the process.

If the requested amount of memory is available on the Heap structure, the following actions will be performed by the new operator–

  • The new operator initializes the requested amount of memory space on the Heap
  • Further, it returns the address of the allocated (initialized) memory to the pointer variable.
  • The pointer variable points and represents the memory space and the value stored at it.

Having understood the working of new operator, let us now focus on the structure of the same.


Syntax of the new operator in C++

We can request memory space using the below command–

pointer-variable = new data-type;

Apart from defining a pointer variable to the requested memory, we can initialize the requested memory to a value.

Syntax:

pointer-variable = new data-type(value);

Apart from requesting memory dynamically, we can even specify the size of memory blocks to be made available.

Syntax:

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

So, in Object-oriented Programming, when we assign an object to a new operator, the following events occur:

  • The new operator allocates memory for the specified object.
  • It invokes the class constructor to initialize the specified memory space.

Examples of C++ new operator

Let’s begin with a quick demo.

We assign a pointer variable(xx) to the integer data-type and allocate the space to the values to be stored into it.

Next, we assign the pointer variable (yy) to the memory allocated for the integer variable with value 12.

#include <iostream>
using namespace std;
int main () {
   int *xx = new int;
   int *yy = new int(12);
  
   cout << "Value of xx pointer variable : " << *xx << endl;
   cout << "Value of yy pointer variable : " << *yy << endl;
 
   
   return 0;
}

Output:

Value of xx pointer variable : 0
Value of yy pointer variable : 12

Now, assign a pointer variable memory space to the float data-type that have a size = 20 bytes.

In the below example, we also assign values to it at a dynamic point of time and displayed the same.

#include <iostream>
using namespace std;
int main () {
  
  float *zz = new float[20];
  for (int x = 1; x<=10; x++)
  {
      zz[x] = x;
  }
  
  cout << "Values stored at the memory pointed by zz pointer variable: "  << endl;
  
   for (int x = 1; x<=10; x++)
  {
      cout<<zz[x]<<" ";
  }
  
   return 0;
}

Output:

Values stored at the memory pointed by zz pointer variable: 
1 2 3 4 5 6 7 8 9 10 

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

Till then, Happy Learning!!


References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages