본문 바로가기

Application-level프로그래밍

(C++) new 연산자 (MSDN)

주의할 점은 할당 실패시의 동작이다.새로운 c++ 표준을 따르는 경우, 반환값이 null 포인터인지 확인하는 것으로는 실패인지 아닌지 감지할 수 없다. 왜냐하면 반환값을 확인하기 전에 exception이 발생하기 때문이다.

The function called by a new-expression to allocate storage for individual objects.

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


Parameters

_Count

The number of bytes of storage to be allocated.

_Ptr

The pointer to be returned.
 

Return value
A pointer to the lowest byte address of the newly-allocated storage. Or
_Ptr.

Remark

The first function is called by a new expression to allocate _Count bytes of storage suitably aligned to represent any object of that size. The program can define an alternate function with this function signature that replaces the default version defined by the Standard C++ Library and so is replaceable.

The required behavior is to return a nonnull pointer only if storage can be allocated as requested. Each such allocation yields a pointer to storage disjoint from any other allocated storage. The order and contiguity of storage allocated by successive calls is unspecified. The initial stored value is unspecified. The returned pointer points to the start (lowest byte address) of the allocated storage. If count is zero, the value returned does not compare equal to any other value returned by the function.

The default behavior is to execute a loop. Within the loop, the function first attempts to allocate the requested storage. Whether the attempt involves a call to malloc(size_t) is unspecified. If the attempt is successful, the function returns a pointer to the allocated storage. Otherwise, the function calls the designated new handler.(메모리 할당이 실패하면 지정된 new 핸들러를 호출) If the called function returns, the loop repeats. The loop terminates when an attempt to allocate the requested storage is successful or when a called function does not return.

The required behavior of a new handler is to perform one of the following operations:

  • Make more storage available for allocation and then return.

  • Call either abort or exit(int).

  • Throw an object of type bad_alloc.
    (new 핸들러에서는 다음 중 하나의 동작을 수행하도록 한다.(요구사항)
    -공간확보 하고 리턴 
    -프로그램 종료
    -bad_alloc 타입의 exception 발생


    (note: exception을 발생하는 것은 함수가 리턴한다고 볼 수 없다. 따라서 오직 공간 확보를 했을 때만 다시 new 함수가 loop를 돌도록 되어있다.)

  • The default behavior of a new handler is to throw an object of type bad_alloc. A null pointer designates the default new handler.

    The order and contiguity of storage allocated by successive calls to operator new(size_t) is unspecified, as are the initial values stored there.

    The second function is called by a placement new expression to allocate _Count bytes of storage suitably aligned to represent any object of that size. The program can define an alternate function with this function signature that replaces the default version defined by the Standard C++ Library and so is replaceable.

    The default behavior is to return operator new(_Count) if that function succeeds. Otherwise, it returns a null pointer.

    The third function is called by a placement new expression, of the form new (args) T. Here, args consists of a single object pointer. This can be useful for constructing an object at a known address. The function returns _Ptr.

    For information on throwing or nonthrowing behavior of new, see The new and delete Operators.