Six new overload Modes

Source: Internet
Author: User
When writing
P = new P ();
Such Code In fact, there are two steps to allocate memory first,
Class Members are initialized on the allocated memory.

The second step is completed by the constructor, and the first step is the work of the new function.

There are six types of global new overload,
Void * operator new (STD: size_t count)
Throw (STD: bad_alloc); // general version

Void * operator new (STD: size_t count, // compatible with earlier versions of new
Const STD: nothrow_t &) Throw (); // if the memory allocation fails, no exception is thrown.

Void * operator new (STD: size_t count, void * PTR) Throw ();
// Placement version
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 ();

Therefore, the usage just now is an overloaded form of using the new function.
If the object a reloads the new function as a member function
Will be called first.
 
Introduction to various new features of C ++

1. New T

The first new method is the simplest. Call the class (if overloaded) or the global operator new to allocate space, and then use
The parameters in the column after the type are used to call the constructor.
New typename (initial_args_list). If no parameter exists, brackets can be omitted. For example:

Int * P = new int;
Int * P = new int (10 );
Int * P = new Foo ("hello ");

Call Delete to destroy:
Delete P;

2. New T []
This new method is used to create a dynamic object array. It will call operator new [] of the object to allocate memory (if
If no, operator new is called, and the search sequence is the same as above). Then, the default constructor of the object is called to initialize each object.
Usage:
New typename [num_of_objects];
For example
Int * P = new int [10];
Use operator Delete [] for destruction

3. New () T and new () T []
This is a new with parameters. In this form, new calls operator new (size_t, othertype) to allocate memory.
Here, the othertype must be compatible with the parameter type in the new brackets,

This syntax is usually used in a specific address component object called Placement new, provided that operator new
(Size_t, void *) has been defined. Generally, the compiler provides an implementation that includes the <New> header file. This
The implementation simply returns the specified address of the parameter. Therefore, the new () operator is created on the address in brackets.
Object

It should be noted that the second parameter is not necessarily void *, which can be recognized as a valid type. At this time, it is reloaded by C ++.
To call the operator new.

Of course, we can provide our own operator new (size _, type) to determine the new behavior, such
Char data [1000] [sizeof (FOO)];
Inline void * operator new (size_t, int N)
{
Return data [N];
}

You can use this interesting syntax to create an object:
Foo * P = new (6) Foo (); // create an object on the sixth unit of data.
Really interesting
The standard library also provides a nothrow implementation:
Void * operator new (STD: size_t, const STD: nothrow_t &) Throw ();
Void * operator new [] (STD: size_t, const STD: nothrow_t &) Throw ();

You can call New without throwing an exception.
New (nothrow) int (10 );
// Nothrow is an instance of STD: nothrow_t.

The objects created by placement new cannot be destroyed directly by Delete. Instead, an object's analysis function must be called to destroy the objects.
For example, how to handle the memory occupied by objects depends on the specific source of the memory.

4. Operator new (size_t)
This operator allocates parameters to specify the memory size and returns the first address. This operator can be reloaded for custom classes,
The method is to declare
Void * operator new (size_t size)
{
// Allocate memory and return its address here
}
Whether declared or not, operator new and operator delete that are reloaded in the class have static attributes.

Generally, you do not need to directly call operator new unless the original memory is allocated directly (this is similar to the malloc of C)
In case of a conflict, you must call the global operator plus: Scope OPERATOR:
: Operator new (1000); // allocate 1000 bytes

If the returned memory needs to be recycled, call the corresponding operator Delete

5. Operator new [] (size_t)

This is also the allocation of memory, but it is dedicated to the array, that is, the new T [] form, of course, you can
Explicit call

6. Operator new (size_t size, othertype other_value)
And operator new [] (size_t size, othertype other_value)
See the new ()

It should be emphasized that, when new is used to create objects and allocate memory, its behavior cannot be changed.
Operator new, we can achieve our memory allocation scheme by reloading operator new.

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.