The new operator and operator new() are closely related but serve different purposes in C++. The new operator allocates memory and constructs an object, whereas operator new() is a function responsible only for allocating raw memory.
- new allocates memory and automatically calls the constructor.
- operator new() allocates raw memory and can be overloaded to customize memory allocation.
new Operator
The new operator dynamically allocates memory from the heap (free store) and initializes the object by calling its constructor.
- Allocates memory and constructs the object in a single step.
- Returns a pointer to the newly created object.
Syntax
pointer = new ClassName(arguments);
Example: The following program demonstrates object creation using the new operator.
#include<iostream>
using namespace std;
class car
{
string name;
int num;
public:
car(string a, int n)
{
cout << "Constructor called" << endl;
this ->name = a;
this ->num = n;
}
void enter()
{
cin>>name;
cin>>num;
}
void display()
{
cout << "Name: " << name << endl;
cout << "Num: " << num << endl;
}
};
int main()
{
// Using new keyword
car *p = new car("Honda", 2017);
p->display();
}
Output
Constructor called Name: Honda Num: 2017
Explanation
- new allocates memory for a Car object.
- The constructor initializes the allocated memory.
- delete destroys the object and releases the allocated memory
operator new()
operator new() is the function that performs raw memory allocation. Unlike the new operator, it does not directly create or initialize an object.
- Allocates raw memory only.
- Can be overloaded to customize memory allocation.
Syntax
void* operator new(size_t size);
Example: The following program overloads operator new() and operator delete() to customize memory allocation.
#include<iostream>
#include<stdlib.h>
using namespace std;
class car
{
string name;
int num;
public:
car(string a, int n)
{
cout << "Constructor called" << endl;
this->name = a;
this->num = n;
}
void display()
{
cout << "Name: " << name << endl;
cout << "Num: " << num << endl;
}
void *operator new(size_t size)
{
cout << "new operator overloaded" << endl;
void *p = malloc(size);
return p;
}
void operator delete(void *ptr)
{
cout << "delete operator overloaded" << endl;
free(ptr);
}
};
int main()
{
car *p = new car("HYUNDAI", 2012);
p->display();
delete p;
}
Output
new operator overloaded Constructor called Name: HYUNDAI Num: 2012 delete operator overloaded
Explanation
- The overloaded operator new() allocates raw memory using malloc().
- After memory allocation succeeds, the constructor is automatically invoked by the new expression.
- operator delete() is called when the object is deleted.
Note: Even when operator new() is overloaded, the constructor is still called automatically by the new expression. Overloading operator new() changes only how memory is allocated, not whether the constructor is called.
new Vs operator new()
The following table summarizes the differences between the two.
| Feature | new | operator new() |
|---|---|---|
| Type | Operator (keyword) | Function |
| Purpose | Allocates memory and constructs an object | Allocates raw memory only |
| Constructor Call | Yes | No (called later by the new expression) |
| Return Type | Pointer to the created object | void* |
| Can Be Overloaded | No | Yes |
| Typical Use | Creating objects | Customizing memory allocation |