Placement new, operator new and new operator

Source: Internet
Author: User

I think these three new ideas are a little interesting. Sometimes I am also confused. It is difficult for these creators to think that C ++ is too simple to come up with vague terms. In this summary, we can provide a reference for further renewal. Please do not hesitate to inform us of anything wrong. Thank you!

Simple:

1. New operator, a familiar New operator, is used to allocate an object from the heap and initialize it. Cheapobject * P = new cheapobject (name );

Since it is an operator, its behavior is the same as other +-*/operators. It is defined by the C ++ language and cannot be changed, even if it is overloaded. C ++ specifies the behavior of the new operator: allocate a suitable space to accommodate cheapobject objects, and then call its constructor to initialize the objects.

2. Operator new is the new operation.Allocate a suitable space to accommodate cheapobject objects"Is completed through the new operation. The global new operation is declared as follows:

Void * operator new (size_t size );

This is a function declaration. In C ++, We can reload this function to change the function's behavior, that is, the way to allocate space. I believe everyone has done this.

In addition, although operator new is called for new operator, it can also be called by you, for example:

Void * P = Operator new (sizeof (cheapobject ));

It only allocates space, equivalent to malloc () in C ().

Although operator new is reloaded, the behavior of new operator in 1 is not changed.

3. Placement new

Alas, I made another offer :-)

This is to call the cheapobject object constructor In the allocated space (such as malloc, operatornew, and returned void * without object information), which is also a special new operation. For example:

Void * buffer = Operator new (100 * sizeof (cheapobject); // allocate space for 100 objects

Cheapobject * construct (void * Buf, string objname)

{

Return new (BUF) cheapobject (objname );

}

This function returns the object pointer, which is allocated on the passed buffer. The usage of this new operator is another use of the new operator. An additional variable Buf is required. When the new operation implicitly calls operator new, the Buf is passed to it, the definition of operator new is as follows:

Void * operator new (size_t, void * buffer)

{

Return buffer;

}

This is placement new.

Let's end with a summary:

If you want to create an object on the stack, you should use the new operator to allocate memory and call constructors for the object.

If you only want to allocate memory, use the operator new function, which does not call the constructor.

If you want to customize your memory allocation process when the heap object is created, reload your own operator new function. The new operator will call your customized operator new.

If you want to create an object in an allocated memory, use placement new.

I think these three new ideas are a little interesting. Sometimes I am also confused. It is difficult for these creators to think that C ++ is too simple to come up with vague terms. In this summary, we can provide a reference for further renewal. Please do not hesitate to inform us of anything wrong. Thank you!

Simple:

1. New operator, a familiar New operator, is used to allocate an object from the heap and initialize it. Cheapobject * P = new cheapobject (name );

Since it is an operator, its behavior is the same as other +-*/operators. It is defined by the C ++ language and cannot be changed, even if it is overloaded. C ++ specifies the behavior of the new operator: allocate a suitable space to accommodate cheapobject objects, and then call its constructor to initialize the objects.

2. Operator new is the new operation.Allocate a suitable space to accommodate cheapobject objects"Is completed through the new operation. The global new operation is declared as follows:

Void * operator new (size_t size );

This is a function declaration. In C ++, We can reload this function to change the function's behavior, that is, the way to allocate space. I believe everyone has done this.

In addition, although operator new is called for new operator, it can also be called by you, for example:

Void * P = Operator new (sizeof (cheapobject ));

It only allocates space, equivalent to malloc () in C ().

Although operator new is reloaded, the behavior of new operator in 1 is not changed.

3. Placement new

Alas, I made another offer :-)

This is to call the cheapobject object constructor In the allocated space (such as malloc, operatornew, and returned void * without object information), which is also a special new operation. For example:

Void * buffer = Operator new (100 * sizeof (cheapobject); // allocate space for 100 objects

Cheapobject * construct (void * Buf, string objname)

{

Return new (BUF) cheapobject (objname );

}

This function returns the object pointer, which is allocated on the passed buffer. The usage of this new operator is another use of the new operator. An additional variable Buf is required. When the new operation implicitly calls operator new, the Buf is passed to it, the definition of operator new is as follows:

Void * operator new (size_t, void * buffer)

{

Return buffer;

}

This is placement new.

Let's end with a summary:

If you want to create an object on the stack, you should use the new operator to allocate memory and call constructors for the object.

If you only want to allocate memory, use the operator new function, which does not call the constructor.

If you want to customize your memory allocation process when the heap object is created, reload your own operator new function. The new operator will call your customized operator new.

If you want to create an object in an allocated memory, use placement 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.