Share via


<new> 함수

get_new_handler

new_handler get_new_handler() noexcept;

설명

현재 new_handler.를 반환합니다.

세탁

template <class T>
    constexpr T* launder(T* ptr) noexcept;

매개 변수

ptr
형식이 T와 유사한 개체를 보유하는 메모리의 바이트 주소입니다.

Return Value

X를 가리키는 T* 형식의 값입니다.

설명

포인터 최적화 장벽이라고도 합니다.

상수 식에서 인수 값을 사용할 수 있는 경우 상수 식으로 사용됩니다. 다른 개체가 차지하는 스토리지 내에서 비슷한 포인터가 있는 개체인 경우 개체를 가리키는 포인터 값을 통해 스토리지 바이트에 연결할 수 있습니다.

예시

struct X { const int n; };

X *p = new X{3};
const int a = p->n;
new (p) X{5}; // p does not point to new object because X::n is const
const int b = p->n; // undefined behavior
const int c = std::launder(p)->n; // OK

nothrow

및 .의 버전 newdelete에 대한 인수로 사용할 개체를 nothrow 제공합니다.

extern const std::nothrow_t nothrow;

설명

이 개체는 매개 변수 형식 std::nothrow_t의 일치를 확인하기 위한 함수 인수로 사용됩니다.

예시

함수 매개 변수로 사용되는 방법 std::nothrow_t 의 예는 참조 operator new 하세요operator new[].

set_new_handler

새 연산자가 메모리 할당 시도에 실패할 때 호출할 사용자 함수를 설치합니다.

new_handler set_new_handler(new_handler Pnew) throw();

매개 변수

Pnew
new_handler 설치할 것입니다.

Return Value

첫 번째 호출의 경우 0이고 후속 호출의 경우 이전 new_handler입니다.

설명

함수는 기본 고정 처리기 포인터에 저장newPnew한 다음 포인터에 이전에 저장된 값을 반환합니다. new 처리기는 .에서 operator new사용됩니다.

예제

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

using namespace std;
void __cdecl newhandler( )
{
   cout << "The new_handler is called:" << endl;
   throw bad_alloc( );
   return;
}

int main( )
{
   set_new_handler (newhandler);
   try
   {
      while ( 1 )
      {
         new int[5000000];
         cout << "Allocating 5000000 ints." << endl;
      }
   }
   catch ( exception e )
   {
      cout << e.what( ) << endl;
   }
}
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
The new_handler is called:
bad allocation