運算子 new (CRT)

從堆積配置記憶體的區塊

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。

運算子 delete [ 釋出使用 new 運算子配置記憶體。

您可以設定是否operator new[] ,則傳回 null,或在失敗時擲回例外狀況。請參閱新增及刪除運算子如需詳細資訊。

擲回的例外情況或不擲回的行為,CRT operator new的行為就像運算子 new [ 標準的 C++ 程式庫。

需求

常式

所需的標頭

new[]

<new.h>

其他的相容性資訊,請參閱相容性在簡介中。

文件庫

所有版本的 C 執行階段程式庫

範例

下列示範如何使用向量、 nonplacement 形式的operator new

// 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;
}

請參閱

參考

記憶體配置