operator new 函数
在程序中遇到以下语句时,它将转换为对函数 operator new 的调用:
备注
char *pch = new char[BUFFER_SIZE];
如果请求针对零字节存储,operator new 将返回一个指向不同的对象的指针(即对 operator new 的重复调用将返回不同的指针)。 如果分配请求没有足够的内存,则 operator new 将返回 NULL 或引发异常(有关详细信息,请参阅 new 和 delete 运算符)。
可以编写尝试释放内存的例程并重试分配;有关详细信息,请参阅 _set_new_handler。 有关恢复方案的更多详细信息,请参阅以下主题:处理内存不足的情况。
下表中描述了 operator new 函数的两个范围。
operator new 函数的范围
运算符 |
范围 |
---|---|
::operator new |
Global |
class-name::operator new |
类 |
operator new 的第一个参数的类型必须为 size_t(STDDEF.H 中定义的类型),并且返回类型始终为 void *。
在使用 new 运算符分配内置类型的对象、不包含用户定义的 operator new 函数的类类型的对象和任何类型的数组时,将调用全局 operator new 函数。 在使用 new 运算符分配类类型的对象时(其中定义了 operator new),将调用该类的 operator new。
为类定义的 operator new 函数是静态成员函数(因此,它不能是虚函数),该函数隐藏此类类型的对象的全局 operator new 函数。 考虑 new 用于分配内存并将内存设为给定值的情况:
// 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;
}
用括号包含的提供给 new 的参数将作为 chInit 参数传递给 Blanks::operator new。 但是,全局 operator new 函数将被隐藏,从而导致以下代码生成错误:
Blanks *SomeBlanks = new Blanks;
在 Visual C++ 5.0 和早期版本中,使用 new 运算符分配的非类类型和所有数组(无论其类型是否为 class)始终使用全局 operator new 函数。
从 Visual C++ 5.0 开始,编译器支持类声明中的成员数组 new 和 delete 运算符。 例如:
// 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;
}