Overloading the new and delete Operators

C++ lets you redefine the behavior of the new and delete operators if you want to perform customized memory management. For example, suppose you want new to initialize the contents of a memory block to zero before returning it. You can implement this by writing special functions named operator new and operator delete. For example:

// Customized new and delete

#include <iostream.h>

#include <stdlib.h>

#include <stddef.h>

// ------------- Overloaded new operator

void *operator new( size_t size )

{

void *rtn = calloc( 1, size );

return rtn;

}

// ----------- Overloaded delete operator

void operator delete( void *ptr )

{

free( ptr );

}

void main()

{

// Allocate a zero-filled array

int *ip = new int[10];

// Display the array

for( int i = 0; i < 10; i++ )

cout << " " << ip[i];

// Release the memory

delete [] ip;

}

Note that the new operator takes an parameter of type size_t. This parameter holds the size of the object being allocated, and the compiler automatically sets its value whenever you use new. Also note that the new operator returns a void pointer. Any new operator you write must have this parameter and return type.

In this particular example, new calls the standard C function calloc to allocate memory and initialize it to zero.

The delete operator takes a void pointer as a parameter. This parameter points to the block to be deallocated. Also note that the delete operator has a void return type. Any delete operator you write must have this parameter and return type.

In this example, delete simply calls the standard C function free to deallocate the memory.

Redefining new to initialize memory this way does not eliminate the call to a class's constructor when you dynamically allocate an object. Thus, if you allocate a Date object using your version of new, the Date constructor is still called to initialize the object after the new operator returns the block of memory.

You can also redefine new to take additional parameters. The following example defines a new operator that fills memory with the character specified when you allocate memory.

// new and delete with character fill

#include <iostream.h>

#include <stdlib.h>

#include <string.h>

#include <stddef.h>

// ------------- Overloaded new operator

void *operator new( size_t size, int filler )

{

void *rtn;

if( (rtn = malloc( size )) != NULL )

memset( rtn, filler, size );

return rtn;

}

// ----------- Overloaded delete operator

void operator delete( void *ptr )

{

free( ptr );

}

void main()

{

// Allocate an asterisk-filled array

char *cp = new( '*' ) char[10];

// Display the array

for( int i = 0; i < 10; i++ )

cout << " " << cp[i];

// Release the memory

delete [] cp;

}

Notice that when you call this version of new, you specify the additional argument in parentheses.

For information about the behavior of new, delete, and set_new_handler in mixed-model programs, see Chapter 5, “Managing Memory in C++,” in Programming Techniques.