Lesson 15 | Overloading new and delete |
Objective | Overload operators new and delete |
Overloading new and delete in C++
Overload the operators new and delete to allow an object to utilize traditional C free store management.
Most classes involve free store memory allocation and deallocation. Most of the time, this is done through simple calls to operators new
and delete
. Sometimes, however, you may need a more sophisticated use of memory for efficiency or robustness.
The operators new
and delete
can be overloaded.
new Operator
One reason to overload these operators is to give them additional semantics, such as providing diagnostic information or making them more fault tolerant.
Also, the class can have a more efficient memory allocation scheme than provided by the system. Up to now, we have been using the global operator new()
to allocate free store. The system provides a sizeof(type)
argument to this function implicitly. Its function prototype is
void* operator new(size_t size);
The operator new can also include placement syntax.
Placement Syntax of the new Operator in C++
The placement syntax provides a comma-separated argument list used to select an overloaded operator new()
with a matching signature. These additional arguments are often used to place the constructed object at a particular address. This form of operator new
uses the new.h
header file.
For example:
//Placement syntax and new overloaded.
#include <iostream.h>
#include <new.h>
char* buf1 = new char[1000]; //in place of free store
char* buf2 = new char[1000];
class object {
public:
.....
private:
.....
};
void main(){
object *p = new(buf1) object; //allocate at buf1
object *q = new(buf2) object; //allocate at buf2
.....
}
Placement syntax allows the user to have an arbitrary signature for the overloaded new
operator.
This signature is distinct from the initializer arguments used by calls to new
that select an appropriate constructor.
The allocator function is operator new or
operator new[],
which can be overloaded.
Two global placement operator new functions are provided by the standard library, you can define additional functions if you wish.
The
allocator function takes a
size_t
as its first parameter, which is the number of bytes of memory to allocate.
It returns a pointer to the memory. The placement syntax is a list of expressions in parentheses. The expression list is passed to the allocator functions after the size argument. The compiler chooses which
overloaded operator new according to the usual rules of overload resolution.
delete operator
The delete
operator has two signatures, either of which can be overloaded:
void operator delete(void* p);
void operator delete(void* p, size_t);
The first signature makes no provision for the number of bytes to be returned by delete
. In this case, the programmer provides code that supplies this value. The second signature includes a size_t
argument passed to the delete
invocation. This is provided by the compiler as the size of the object pointed at by p
. Only one form of delete
can be provided as a static
member function in each class.
new-delete Operators - Exercise
Click the Exercise link below to overload the operators
new
and
delete
to allow an object to utilize traditional C free store management.
new-delete operators - Exercise