The new and delete operators can also be overloaded like other operators in C++. New and Delete operators can be overloaded globally or they can be overloaded for specific classes.
- If these operators are overloaded using member function for a class, it means that these operators are overloaded only for that specific class.
- If overloading is done outside a class (i.e. it is not a member function of a class), the overloaded ‘new’ and ‘delete’ will be called anytime you make use of these operators (within classes or outside classes). This is global overloading.
Syntax for overloading the new operator :
void* operator new(size_t size);
The overloaded new operator receives size of type size_t, which specifies the number of bytes of memory to be allocated. The return type of the overloaded new must be void*.The overloaded function returns a pointer to the beginning of the block of memory allocated.
Syntax for overloading the delete operator :
void operator delete(void*);
The function receives a parameter of type void* which has to be deleted. Function should not return anything.
NOTE: Both overloaded new and delete operator functions are static members by default. Therefore, they don’t have access to this pointer .
Overloading new and delete operator for a specific class
Overloading new operator with size: 16 Constructor is called Name:Yash Age:24 Overloading delete operator
NOTE: In the above new overloaded function, we have allocated dynamic memory through new operator, but it should be global new operator otherwise it will go in recursion
void *p = new student(); // this will go in recursion asnew will be overloaded again and again
void *p = ::new student(); // this is correct
Global overloading of new and delete operator
New operator overloading Array: 0 1 2 3 4 Delete operator overloading
NOTE: In the code above, in new overloaded function we cannot allocate memory using ::new int as it will go in recursion. We need to allocate memory using malloc only.
Why to overload new and delete?
- The overloaded new operator function can accept arguments; therefore, a class can have multiple overloaded new operator functions. This gives the programmer more flexibility in customizing memory allocation for objects. For example:
*ptr = c;
NOTE: Code will not only allocate memory for single character but will also initialize the allocated memory with # character.
- Overloaded new or delete operators also provide Garbage Collection for class’s object.
- Exception handling routine can be added in overloaded new operator function.
- Sometimes you want operators new and delete to do something customized that the compiler-provided versions don’t offer. For example, You might write a custom operator delete that overwrites deallocated memory with zeros in order to increase the security of application data.
- We can use realloc() function in new function to re-allocate memory dynamically.
- Overloaded new operator also enables programmers to squeeze some extra performance out of their programs. For example, In a class, to speed up the allocation of new nodes, a list of deleted nodes is maintained so that their memory can be reused when new nodes are allocated.In this case, the overloaded delete operator will add nodes to the list of deleted nodes and the overloaded new operator will allocate memory from this list rather than from the heap to speedup memory allocation. Memory from the heap can be used when the list of deleted nodes is empty.
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.
- Operator Overloading '<<' and '>>' operator in a linked list class
- new vs operator new in C++
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Increment (++) and Decrement (--) operator overloading in C++
- Rules for operator overloading
- Operator Overloading in C++
- Overloading Subscript or array index operator  in C++
- C++ | Operator Overloading | Question 10
- C++ Program to concatenate two strings using Operator Overloading
- Operator overloading in C++ to print contents of vector, map, pair, ..
- Types of Operator Overloading in C++
- C++ program to compare two Strings using Operator Overloading
- Count number of Unique Triangles using Operator overloading
- Overloading the Comma Operator
- new and delete operators in C++ for dynamic memory
- new vs malloc() and free() vs delete in C++
- Placement new operator in C++
- vector::operator= and vector::operator[ ] in C++ STL
- deque::operator= and deque::operator in C++ STL
- 3-way comparison operator (Space Ship Operator) in C++ 20
Improved By : JamesHammer