C++ 中new 有三种用法,分别是:plain new, nothrow new, placement new

 

plain new

1 void* operator new(std::size_t) throw(std::bad_alloc);  

2 void operator delete( void *) throw();

plain new在分配失败的情况下,

抛出异常std::bad_alloc而不是返回NULL,

因此通过判断返回值是否为NULL是徒劳的

 

nothrow new

1 void * operator new(std::size_t, const std::nothrow_t&) throw();

2 void operator delete(void*) throw();

nothrow new在失败时,返回NULL

是不抛出异常的new的形式

 

placement new

1 void* operator new(size_t, void*);

2 void operator delete(void*, void*);

主要用途是反复使用一块较大的动态分配的内存来构造不同类型的对象或者他们的数组。

placement new构造起来的对象或其数组

要显示的调用他们的析构函数来销毁

千万不要使用delete。

 

 

 

//涉足尚浅,如有不当,欢迎指出

相关文章:

  • 2021-07-12
  • 2022-12-23
  • 2021-10-06
  • 2021-11-07
  • 2022-12-23
  • 2021-12-16
  • 2021-10-04
猜你喜欢
  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
  • 2021-09-25
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案