operator new

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 the request is for zero bytes of storage, operator new returns a pointer to a distinct object (that is, repeated calls to operator newreturn different pointers). If there is insufficient memory for the allocation request, operator new returns NULL or throws an exception (see The new and delete Operators for more information).

You can write a routine that attempts to free memory and retry the allocation; see _set_new_handler for more information. For more details on the recovery scheme, see the following topic, Handling Insufficient Memory Conditions.

The two scopes for operator new functions are described in the following table.

Scope for operator new Functions

OperatorScope
::operator newGlobal
class-name::operator newClass

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-definedoperator 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:

// spec1_the_operator_new_function1.cpp
#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
int 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;

In Visual C++ 5.0 and earlier, nonclass types and all arrays (regardless of whether they were of class type) allocated using the new operator always used the globaloperator new function.

Beginning with Visual C++ 5.0, the compiler supports member array new and delete operators in a class declaration. For example:

// spec1_the_operator_new_function2.cpp
class MyClass
{
public:
   void * operator new[] (size_t)
   {
      return 0;
   }
   void   operator delete[] (void*)
   {
   }
};

int main() 
{
   MyClass *pMyClass = new MyClass[5];
   delete [] pMyClass;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值