Share via


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 바이트의 저장소를 적절 하 게 정렬 된 배열 개체를 해당 크기를 나타내는 또는 더 작게.프로그램 표준 C++ 라이브러리에서 정의 된 기본 버전 대신이 함수 시그니처가 있는 함수를 정의할 수 있습니다.필요한 동작을 동일 새 연산자(size_t).기본 동작을 반환 하는 것 operator new(_Count).

두 번째 함수를 배치 하 라고 new[] 할당 식 _Count 바이트의 저장소를 적절 하 게 정렬 배열 개체의 크기를 나타내는 데.프로그램 표준 C++ 라이브러리에서 정의 된 기본 버전 대신이 함수 시그니처가 있는 함수를 정의할 수 있습니다.기본 동작을 반환 하는 것 운영자새(_Count) 해당 함수가 성공적으로 실행 하는 경우.그렇지 않은 경우 null 포인터를 반환합니다.

세 번째 함수를 배치 하 라고 new[] 형태의 식 (args) TN.여기에서 args 단일 개체 포인터의 구성 됩니다.함수 반환 _Ptr.

저장소 할당 늘리려면 **operator new[]**을 호출 delete 연산자.

Throw 하는 방법에 대 한 나 새, 참조 동작 nonthrowing 새 연산자를 삭제 하 고.

예제

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

요구 사항

헤더: <new>

네임 스페이스: std

참고 항목

참조

nothrow_t Structure

operator delete[] (<new>)