新operator[] (crt)

allocate 阻止堆中的内存

void *__cdecl operator new[](
   size_t count
);
void *__cdecl operator new[] (
   size_t count, 
   void * object
) throw();
void *__cdecl operator new[] (
   size_t count, 
   const std::nothrow_t&
) throw();

参数

  • 计数
    已分配大小。

  • object
    对象将创建内存块的指针。

返回值

为新分配存储的最低的字节地址的指针。

备注

operator new 的此窗体称为向量新,与个标量新窗体 (new 运算符) 相比

此运算符的第一个窗体称为 nonplacement 窗体。 此运算符的第二个窗体称为位置窗体,该运算符的第三个窗体是 nonthrowing 的放置形式。

运算符的第一个窗体是由编译器在过程定义中,不需要 new.h 中。

运算符删除 [] 释放内存分配使用 new 运算符。

可以配置 operator new[] 是否返回 null 或在失败时引发的异常。 请参见 新建和删除运算符 有关更多信息。

除了引发的或由引发的行为外, CRT operator new 行为与 新 operator[] 对标准 C++ 库中。

要求

实例

必需的头

new[]

new.h

有关其他的兼容性信息,请参见中介绍的 兼容性

C 运行库的所有版本。

示例

下面的示例演示如何使用向量, operator new的 nonplacement 窗体。

// crt_new4.cpp
#include <stdio.h>
int main() {
   int * k = new int[10];
   k[0] = 21;
   printf("%d\n", k[0]);
   delete [] k;
}

下面的示例演示如何使用向量, operator new的放置形式。

// crt_new5.cpp
#include <stdio.h>
#include <new.h>
int main() {
   int * i = new int[10];
   i[0] = 21;
   printf("%d\n", i[0]);
   // initialize existing memory (i) with, in this case, int[[10]
   int * j = new(i) int[10];   // placement vector new
   printf("%d\n", j[0]);
   j[0] = 22;
   printf("%d\n", i[0]);
   delete [] i;   // or, could have deleted [] j 
}

下面的示例演示如何使用向量,位置, operator new非引发窗体。

// crt_new6.cpp
#include <stdio.h>
#include <new.h>
int main() {
   int * k = new(std::nothrow) int[10];
   k[0] = 21;
   printf("%d\n", k[0]);
   delete [] k;
}

请参见

参考

内存分配