C++ New 库 - operator delete
描述
它释放存储空间。
声明
以下是 operator new[] 的声明。
void operator delete (void* ptr) throw(); (ordinary delete) void operator delete (void* ptr, const std::nothrow_t& nothrow_constant) throw(); (nothrow delete) void operator delete (void* ptr, void* voidptr2) throw(); (placement delete)
C++11
void operator delete (void* ptr) noexcept; (ordinary delete) void operator delete (void* ptr, const std::nothrow_t& nothrow_constant) noexcept; (nothrow delete) void operator delete (void* ptr, void* voidptr2) noexcept; (placement delete)
C++14
void operator delete (void* ptr) noexcept; (ordinary delete) void operator delete (void* ptr, const std::nothrow_t& nothrow_constant) noexcept; (nothrow delete) void operator delete (void* ptr, void* voidptr2) noexcept; (placement delete) void operator delete (void* ptr, std::size_t size) noexcept; (ordinary delete with size) void operator delete (void* ptr, std::size_t size, const std::nothrow_t& nothrow_constant) noexcept; (ordinary delete with size)
参数
size − 它包含所请求内存块的大小(以字节为单位)。
nothrow_value − 它包含常量 nothrow。
ptr − 它是一个指向适当大小的已分配内存块的指针。
voidptr2 − 它是一个 void 空指针。
返回值
none
异常
No-throw guarantee − 此函数从不抛出异常。
数据竞争
它修改返回值引用的存储。
示例
在下面的例子中解释了 new 操作符。
#include <iostream> struct MyClass { MyClass() {std::cout <<"It is a MyClass() constructed\n";} ~MyClass() {std::cout <<"It is a MyClass() destroyed\n";} }; int main () { MyClass * pt = new (std::nothrow) MyClass; delete pt; return 0; }
让我们编译并运行上面的程序,这将产生以下结果 −
It is a MyClass() constructed It is a MyClass() destroyed