When you create a new object, memory is allocated using operator new function and then the constructor is invoked to initialize the memory. Here, The new operator does both the allocation and the initialization, where as the operator new only does the allocation.
Let us see how these both work individually.
The new operator is an operator which denotes a request for memory allocation on the Heap. If sufficient memory is available, new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable. When you create an object of class using new keyword(normal new).
- The memory for the object is allocated using operator new from heap.
- The constructor of the class is invoked to properly initialize this memory.
Constructor called Name: Honda Num: 2017
Operator new is a function that allocates raw memory and conceptually a bit similar to malloc().
- It is the mechanism of overriding the default heap allocation logic.
- It doesn’t initializes the memory i.e constructor is not called. However, after our overloaded new returns, the compiler then automatically calls the constructor also as applicable.
- It’s also possible to overload operator new either globally, or for a specific class
new operator overloaded Constructor called Name:HYUNDAI Num:2012 delete operator overloaded
New operator vs operator new
- Operator vs function: new is an operator as well as a keyword whereas operator new is only a function.
- New calls “Operator new”: “new operator” calls “operator new()” , like the way + operator calls operator +()
- “Operator new” can be Overloaded: Operator new can be overloaded just like functions allowing us to do customized tasks.
- Memory allocation: ‘new expression’ call ‘operator new’ to allocate raw memory, then call constructor.
This article is contributed by Yash Singla. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- vector::operator= and vector::operator[ ] in C++ STL
- deque::operator= and deque::operator in C++ STL
- Operator Overloading '<<' and '>>' operator in a linked list class
- 3-way comparison operator (Space Ship Operator) in C++ 20
- new operator vs newInstance() method in Java
- Placement new operator in C++
- Overloading New and Delete operator in c++
- Operands for sizeof operator
- Copy constructor vs assignment operator in C++
- Self assignment check in assignment operator
- Rules for operator overloading
- Result of comma operator as l-value in C and C++
- Comma operator should be used carefully
- C/C++ Ternary Operator - Some Interesting Observations
- Is assignment operator inherited?
- When should we write our own assignment operator in C++?
- Default Assignment Operator and References
- Can we use % operator on floating point numbers?
- Set a variable without using Arithmetic, Relational or Conditional Operator