- C 标准库
 - C 标准库
 
- C++ 标准库
 - C++ 库 - 主页
 - C++ 库 - <fstream>
 - C++ 库 - <iomanip>
 - C++ 库 - <ios>
 - C++ 库 - <iosfwd>
 - C++ 库 - <iostream>
 - C++ 库 - <istream>
 - C++ 库 - <ostream>
 - C++ 库 - <sstream>
 - C++ 库 - <streambuf>
 - C++ 库 - <原子>
 - C++ 库 - <复杂>
 - C++ 库 - <异常>
 - C++ 库 - <功能>
 - C++ 库 - <限制>
 - C++ 库 - <语言环境>
 - C++ 库 - <内存>
 - C++ 库 - <新>
 - C++ 库 - <数字>
 - C++ 库 - <正则表达式>
 - C++ 库 - <stdexcept>
 - C++ 库 - <字符串>
 - C++ 库 - <线程>
 - C++ 库 - <元组>
 - C++ 库 - <类型信息>
 - C++ 库 - <实用程序>
 - C++ 库 - <valarray>
 
C++ 新库 - 运算符 new[]
描述
它为数组分配存储空间。
宣言
以下是运算符 new[] 的声明。
void* operator new[] (std::size_t size) throw (std::bad_alloc); (throwing allocation) void* operator new[] (std::size_t size, const std::nothrow_t& nothrow_value) throw(); (nothrow allocation) void* operator new[] (std::size_t size, void* ptr) throw(); (placement)
C++11
void* operator new[] (std::size_t size); (throwing allocation) void* operator new[] (std::size_t size, const std::nothrow_t& nothrow_value) noexcept; (nothrow allocation) void* operator new[] (std::size_t size, void* ptr) noexcept; (placement)
参数
size - 它包含请求的内存块的大小(以字节为单位)。
nothrow_value - 它包含常量 nothro。
ptr - 它是指向已分配的适当大小的内存块的指针。
返回值
它返回一个指向新分配的存储空间的指针。
例外情况
如果分配存储失败,则会抛出 bad_alloc。
数据竞赛
它修改返回值引用的存储。
例子
在下面的示例中解释了 new 运算符。
#include <iostream>
#include <new>
struct MyClass {
   int data;
   MyClass() {std::cout << '@';}
};
int main () {
   std::cout << "constructions (1): ";
   MyClass * p1 = new MyClass[10];
   std::cout << '\n';
   std::cout << "constructions (2): ";
   MyClass * p2 = new (std::nothrow) MyClass[5];
   std::cout << '\n';
   delete[] p2;
   delete[] p1;
   return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
constructions (1): @@@@@@@@@@ constructions (2): @@@@@
新的.htm