详解C++中new运算符和delete运算符的使用

2020-01-06 14:37:54王振洲

 

 

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 的参数将作为 Blanks::operator new 参数传递给 chInit。但是,全局 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;
}

处理内存不足
对失败的内存分配进行测试可以通过如下编码实现:


// insufficient_memory_conditions.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
#define BIG_NUMBER 100000000
int main() {
 int *pI = new int[BIG_NUMBER];
 if( pI == 0x0 ) {
  cout << "Insufficient memory" << endl;
  return -1;
 }
}

处理失败的内存分配要求的其他方法:编写自定义恢复例程来处理此类失败,然后通过调用 _set_new_handler 运行时函数来注册您的函数。