In our series of C++ programming, in this article, we talk about the new operator in C++.
So, let us begin!
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–
Having understood the working of new operator, let us now focus on the structure of the same.
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:
#c++ #cplusplus #programming-c