operator new[] (<new>)

新的運算式呼叫的配置功能配置陣列儲存物件。

void *operator new[]( 
   std::size_t _Count 
) 
   throw(std::bad_alloc); 
void *operator new[]( 
   std::size_t _Count, 
   const std::nothrow_t& 
) throw( ); 
void *operator new[]( 
   std::size_t _Count,  
   void* _Ptr 
) throw( );

參數

  • _Count
    位元組數目為陣列物件將配置的儲存區。

  • _Ptr
    要傳回的指標。

傳回值

新配置儲存區之最低位元組位址的指標。 或 _Ptr.

備註

第一個函式由 new[] 運算式呼叫配置儲存體一些物件地對齊的 _Count 位元組表示大小所有陣列物件或更小。 程式可以定義與取代 Standard C++ 程式庫所定義的預設版本上執行此函式簽章的函式。 必要的行為是一樣的 new 運算子(size_t)。 預設行為是傳回 operator new(_Count)。

第二個函式會將 new[] 運算式呼叫配置儲存體一些物件地對齊的 _Count 位元組表示大小所有陣列物件。 程式可以定義與取代 Standard C++ 程式庫所定義的預設版本上執行此函式簽章的函式。 預設行為是傳回 operator new(_Count),則該函式成功。 否則,會傳回 null 指標。

第三個函式將由 new[] 運算式,表單呼叫 new Type(args) T[N]。 在此例中, 引數 包含單一物件指標。 函式會傳回 _Ptr。

對 **operator new[]**配置空間閒置儲存,呼叫 operator delete [

如需擲回的或 nonthrowing 行為的新資訊,請參閱 new 和 delete 運算子

範例

// new_op_alloc.cpp
// compile with: /EHsc
#include <new>
#include <iostream>

using namespace std;

class MyClass {
public:
   MyClass() {
      cout << "Construction MyClass." << this << endl;
   };

   ~MyClass() {
      imember = 0; cout << "Destructing MyClass." << this << endl;
      };
   int imember;
};

int main() {
   // The first form of new delete
   MyClass* fPtr = new MyClass[2];
   delete[ ] fPtr;

   // The second form of new delete
   char x[2 * sizeof( MyClass ) + sizeof(int)];
   
   MyClass* fPtr2 = new( &x[0] ) MyClass[2];
   fPtr2[1].~MyClass();
   fPtr2[0].~MyClass();
   cout << "The address of x[0] is : " << ( void* )&x[0] << endl;

   // The third form of new delete
   MyClass* fPtr3 = new( nothrow ) MyClass[2];
   delete[ ] fPtr3;
}

範例輸出

Construction MyClass.00311AEC
Construction MyClass.00311AF0
Destructing MyClass.00311AF0
Destructing MyClass.00311AEC
Construction MyClass.0012FED4
Construction MyClass.0012FED8
Destructing MyClass.0012FED8
Destructing MyClass.0012FED4
The address of x[0] is : 0012FED0
Construction MyClass.00311AEC
Construction MyClass.00311AF0
Destructing MyClass.00311AF0
Destructing MyClass.00311AEC

需求

新 <的標題: >

命名空間: std

請參閱

參考

nothrow_t 結構

operator delete[] (<new>)