C++ 新库 - 运算符删除[]


描述

它释放数组的存储空间。

宣言

以下是运算符delete[] 的声明。

		
void operator delete[] (void* ptr) throw();    (ordinary delete)
void operator delete[] (void* ptr, const std::nothrow_t& nothrow_constant) throw();     (ordinary 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 - 它包含常量 nothro。

  • ptr - 它是指向已分配的适当大小的内存块的指针。

  • voidptr2 - 它是一个空指针。

返回值

没有任何

例外情况

无抛出保证- 该函数永远不会抛出异常。

数据竞赛

它修改返回值引用的存储。

例子

在下面的例子中解释了新的运算符delete[]。

#include <iostream>

struct MyClass {
   MyClass() {std::cout <<"MyClass is constructed\n";}
   ~MyClass() {std::cout <<"MyClass is destroyed\n";}
};

int main () {
   MyClass * pt;

   pt = new MyClass[3];
   delete[] pt;

   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果 -

MyClass is constructed
MyClass is constructed
MyClass is constructed
MyClass is destroyed
MyClass is destroyed
MyClass is destroyed
新的.htm