operator new(<new>)
개별 개체에 대해 저장소를 할당하기 위해 새로운 식에서 호출되는 함수입니다.
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( );
매개 변수
_Count
할당할 저장소의 바이트 수입니다._Ptr
The pointer to be returned.
반환 값
새로 할당된 저장소에서 가장 낮은 바이트 주소의 포인터입니다. Or _Ptr.
설명
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. 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.
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.
To free storage allocated by operator new, call operator delete.
For information on throwing or nonthrowing behavior of new, see The new and delete Operators.
예제
// new_op_new.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;
delete fPtr;
// The second form of new delete
MyClass* fPtr2 = new( nothrow ) MyClass;
delete fPtr2;
// The third form of new delete
char x[sizeof( MyClass )];
MyClass* fPtr3 = new( &x[0] ) MyClass;
fPtr3 -> ~MyClass();
cout << "The address of x[0] is : " << ( void* )&x[0] << endl;
}
Example Output
Construction MyClass.000B3F30
Destructing MyClass.000B3F30
Construction MyClass.000B3F30
Destructing MyClass.000B3F30
Construction MyClass.0023FC60
Destructing MyClass.0023FC60
The address of x[0] is : 0023FC60
요구 사항
Header: <new>
네임스페이스: std