Operator New Function

Source: Internet
Author: User
Document directory
  • Parameters
  • Return Value
Operator new
void* operator new (std::size_t size) throw (std::bad_alloc);void* operator new (std::size_t size, const std::nothrow_t& nothrow_constant) throw();void* operator new (std::size_t size, void* ptr) throw();

Allocate storage space

The first version allocatesSizeBytes of storage space, aligned to represent an object of that size, and returns a non-null pointer to the first byte of this block. On failure, it throws abad_alloc
Exception.

The first version allocates a size byte of storage space, and the returned value points to the first byte of the allocated space. If the allocation fails, a bad_alloc exception is thrown.

The second version isNothrow version. It does the same as the first version, doesn't that on failure it returns a null pointer instead of throwing an exception.

The second version is a nothrow version. The basic operation is the same as the first version. Except when the allocation fails, it returns a null pointer instead of throwing an exception.

The third version isPlacement version, That does not allocate memory-it simply returnsPTR. Notice though that the constructor for the object (if any) will still be called by the operator expression.

The third version is placement new, which does not allocate memory -- it only returns PTR.Note: The object constructor will still be called.

Global Dynamic Storage operator functions are special in the Standard Library:

  • All three versionsOperator newAre declared in the global namespace, not inSTDNamespace.
  • The first and second versions areImplicitly declaredIn every translation unit of a C ++ program: The Header<New>Does not need to be encoded for them to be present.
  • The first and second versions are alsoReplaceable: A program may provide its own definition, that replaces the default one, to produce the result described above.

If set_new_handler has been used to define anew_handler function, this
New_handlerFunction is called by the standard default definition
Operator newIf it cannot allocate the requested storage by its own.

Operator newCan be called explicitly as a regular function, but in C ++,NewIs an operator with a very specific behavior: an expression withNewOperator, first CILS Function
Operator newWith the size of its type specifier as first argument, and if this is successful, it then automatically initializes or constructs the object (if needed ). finally, the expression evaluates as a pointer to the appropriate type.

Operator new functions can be explicitly called as a normal function, but in C ++, new is a very special operator. For a new operation expression, first, call the function operator new and use the size of the corresponding type to initialize the first parameter. If the parameter is successful, it automatically calls the object's constructor and then returns the appropriate type of pointer to the expression.

Note:

In C ++, the new operator can be overloaded to receive more than one parameter: the first parameter passed to operator New is always the allocated memory size of the element type, however, other parameters can be passed through brackets. For example:

 
int * p = new (x) int;

This expression will call



Operator new (sizeof (INT), X );

Parameters
Size
Size in bytes of the requested memory block.
Size_t is an integral type.
Nothrow_constant
The constant nothrow.
This parameter is only used to distinguish it from the first version with an overloaded version. When thenothrow constant is passed as second parameter Operator new,
Operator newReturns a null-pointer on failure instead of throwing abad_alloc exception.
Nothrow_t is the type of constantnothrow.
PTR
A pointer to a memory block where the object is to be constructed

Return Value

For the first and second versions, a pointer to the newly allocated storage space.

For the third version,PTRIs returned.

// operator new example#include <iostream>#include <new>using namespace std;struct myclass {myclass() {cout <<"myclass constructed\n";}};int main () {   int * p1 = new int;// same as:// int * p1 = (int*) operator new (sizeof(int));   int * p2 = new (nothrow) int;// same as:// int * p2 = (int*) operator new (sizeof(int),nothrow);   myclass * p3 = (myclass*) operator new (sizeof(myclass));// (!) not the same as:// myclass * p3 = new myclass;// (constructor not called by function call, even for non-POD types)   new (p3) myclass;   // calls constructor// same as:// operator new (sizeof(myclass),p3)   return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.