新运算符(crt)

allocates 阻止堆中的内存

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[]) 相比, operator new 的此窗体称为标量新,。

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

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

delete 运算符 释放内存分配的 operator new

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

除了引发的或由引发的行为外, CRT operator new 行为与 new 运算符 对标准 C++ 库中。

要求

实例

必需的头

new

new.h

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

C 运行库的所有版本。

示例

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

// crt_new1.cpp
#include <stdio.h>
int main() {
   int * i = new int(6);
   printf("%d\n", *i);
   delete i;
}

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

// crt_new2.cpp
#include <stdio.h>
#include <new.h>
int main() {
   int * i = new int(12);
   printf("*i = %d\n", *i);
   // initialize existing memory (i) with, in this case, int(7)
   int * j = new(i) int(7);   // placement new
   printf("*j = %d\n", *j);
   printf("*i = %d\n", *i);
   delete i;   // or, could have deleted j
}

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

// crt_new3.cpp
#include <stdio.h>
#include <new.h>
int main() {
   // allocates memory, initialize (8) and if call fails, new returns null
   int * k = new(std::nothrow) int(8);   // placement new
   printf("%d\n", *k);
   delete k;
}

.NET Framework 等效项

不适用。若要调用标准 C 函数,请使用 PInvoke。有关更多信息,请参见 平台调用示例

请参见

参考

内存分配