The operator new Function

When a statement such as the following is encountered in a program, it translates into a call to the function operator new:

char *pch = new char[BUFFER_SIZE];

If there is insufficient memory for the allocation request, operator new returns NULL. However, if the request is for zero bytes of storage, operator new returns a pointer to a distinct object (that is, repeated calls to operator new return different pointers).

There are two scopes for operator new functions. They are described in Table 11.4.

Table 11.4 Scope for operator new Functions

Operator Scope

::operator new Global
class-name::operator new Class

The first argument to operator new must be of type size_t (a type defined in STDDEF.H), and the return type is always void *.

The global operator new function is called when the new operator is used to allocate objects of built-in types, objects of class type that do not contain user-defined operator new functions, and arrays of any type. When the new operator is used to allocate objects of a class type where an operator new is defined, that class's operator new is called.

An operator new function defined for a class is a static member function (which cannot, therefore, be virtual) that hides the global operator new function for objects of that class type. Consider the case where new is used to allocate and set memory to a given value:

#include <malloc.h>

#include <memory.h>

class Blanks

{

public:

Blanks(){}

void *operator new( size_t stAllocateBlock, char chInit );

};

void *Blanks::operator new( size_t stAllocateBlock, char chInit )

{

void *pvTemp = malloc( stAllocateBlock );

if( pvTemp != 0 )

memset( pvTemp, chInit, stAllocateBlock );

return pvTemp;

}

For discrete objects of type Blanks, the global operator new function is hidden. Therefore, the following code allocates an object of type Blanks and initializes it to 0xa5:

main()

{

Blanks *a5 = new( 0xa5 ) Blanks;

return a5 != 0;

}

The argument supplied in parentheses to new is passed to Blanks::operator new as the chInit argument. However, the global operator new function is hidden, causing code such as the following to generate an error:

Blanks *SomeBlanks = new Blanks;

Nonclass types and all arrays (regardless of whether they are of class type) allocated using the new operator always use the global operator new function.

Microsoft Specific

The operator new function can be overloaded on its return type to allow specification of different new operators for different memory models. Table 11.5 shows the various function declarations for operator new. (For more information about overloading, see Chapter 12, “Overloading.”)¨

Table 11.5 Declarations for new Operator

Heap Declaration

Default void *operator new( size_t ); The argument is the amount of storage (in bytes) to allocate.
Near void __near *operator new( size_t ); The argument is the amount of storage (in bytes) to allocate.
Far void __far *operator new( size_t ); The argument is the amount of storage (in bytes) to allocate.
Huge void __huge *operator new( unsigned long, size_t ); The first argument is the number of elements to allocate, and the second argument is the size of a given element (in bytes).
Based void __based( void ) *operator new( __segment, size_t ); The first argument is the segment specified in the new expression, and the second argument is the amount of storage (in bytes) to allocate. For example, the expression: new __based( __segname( "_TEXT" ) ) int calls operator new with the segment value corresponding to _TEXT and a size equal to sizeof( int ).

Note:

The operator new function is the only function that can be overloaded solely on the basis of return type. It can be overloaded only in the forms shown in Table 11.5.