Introduction to new in C++
new is an operator in C++ which requests for allocation of memory on the heap area of the stack. If there is not enough memory available, then the new operator will initialize the memory and will return the address to the newly allocated memories with the initialization of memory to the pointer variable. When an object is created it is always necessary to create some memory to be allocated to the object from the heap area of the stack and the constructor of the class gets provoked once the memory is allocated.
Syntax
Ptr_var = new data_type;
- Ptr_var: It represents the pointer variable pointing to the datatype.
- new: keyword used for the creation of the memory cells and allocating it to the data_type.
- data_type: Data type could be any kind of data like built-in data type including array, structure, and class.
How do new operators work in C++?
- Whenever a new keyword comes into the picture it designates that the memory allocation Is the current requirement of the variable taken into consideration. There is a difference between new and operator new with a fact that the new operator does both allocation of memory and the initialization whereas the operator only does the allocation.
- Operator new is a function which is used for allocating memory and conceptually a bit which is smaller to malloc. Malloc is the method of overriding the heap allocation logic by default. It doesn’t initialize any memory which means It doesn’t call any constructor, but once an overloaded operator gets called it returns new and then the compiler automatically calls for the constructor as applicable. Overloading of the operator is possible and can be done globally or can be done for a specific class.
- There are also some differences that must be kept in mind very appropriately like new is an operator as well as a keyword where the operator new is only a function. new operator calls an operator new similar to the way where + operator calls for the operator+(). There is another very interesting fact that the operator new and new keyword persist and the fact is that the Operator new allows all the programmers to make any type of customized tasks and thus it helps in overloading the functions which creates an enhanced and interesting feature for the users.
- Moreover, in C++ new is an operator with very precise behavior what it does is that it first calls for expression with the new operator and function with the size of the type it specifies for the first argument into account. If in case this function becomes successful, then it will automatically call and will start initialization for the construction of the object (urgently).
- At least the expression evaluates as a pointer to the appropriate type. new operator’s main task or objective is to allocate storage space with default allocations functions. Further, the default allocation functions are mentioned which includes throwing allocation, nothrow allocation, and placement. Throwing allocation as part of the new operator deals with the size bytes that get allocated to the storage space and then it suitably gets attached with the object of that size to return any non-null pointer for the very first byte of the block and then if in case it fails for the allocation it throws a bad allocation exception.
- In the case of nothrow allocation, the behavior or the exception throw is the same although it makes the inclusion of no throw that will return a null exception. At last, comes into picture the placement as a default allocation of storage which simply returns ptr where no storage is allocated and then if the function gets called by a new-expression then the proper initialization is needed to be performed with the fact that it includes for calling the default constructor.
- These default constructors play a major role in the new operator with allocation and deallocation of functions with special components in the standard library which includes several unique properties like global which includes all the three versions of the operator and is used for declaring in the global namespace with Implicit and Replaceable functions majorly acting as part of the operator new. All the parameters and return type value for the first and second versions involve pointer to the newly allocated storage space, but the placement involves the ptr as the return type for the new operator invocation in terms of space allocation.
Examples to Implement new in C++
Below are the examples to implement new in C++:
Example #1
This program is used for illustrating the new keyword usage with the class created as Scooty which in turn is described with the call of new keyword at the end which is shown with the given output.
Code:
#include<iostream>
using namespace std;
class Scooty
{
string nm;
int no;
public:
Scooty(string k, int l)
{
cout << "Call_For_Constructor" << endl;
this ->nm = k;
this ->no = l;
}
void enter()
{
cin>>nm;
cin>>no;
}
void display()
{
cout << "Enter_Name: " << nm << endl;
cout << "Enter_No: " << no << endl;
}
};
int main()
{
Scooty *p_r = new Scooty("Activa", 2013);
p_r->display();
}
Output:
Example #2
This program is used to demonstrate the usage of operator new in C++ with the overloading operations as shown in the output.
Code:
#include<iostream>
#include<stdlib.h>
using namespace std;
class Cycle
{
string n_m;
int n_o;
public:
Cycle(string m, int u)
{
cout << "Call_for_constructor" << endl;
this->n_m = m;
this->n_o = u;
}
void display()
{
cout << "Enter_Name: " << n_m << endl;
cout << "Enter_Num: " << n_o << endl;
}
void *operator new(size_t size)
{
cout << "Overload the new_Operator." << endl;
void *pt = malloc(size);
return pt;
}
void operator delete(void *ptr)
{
cout << "Deletion of Overloaded Operator." << endl;
free(ptr);
}
};
int main()
{
Cycle *p = new Cycle("Ladybird", 2009);
p->display();
delete p;
}
Output:
Conclusion
The new in C++ is a keyword which is mostly used for allocation of memory and storage space if considered with the operators in new both has mere differences but still, functionality-wise both are almost same and do comply with each other for the tuning and sync of the storage simultaneously performing initialization of variables dynamically.
Recommended Articles
This is a guide to new in C++. Here we discuss how does it works with examples to implement with proper coding. You can also go through our other related articles to learn more –