Summary: three methods of using "new" in C ++

Source: Internet
Author: User
Post from:! 64a26545e8622b86! 460. entry summary three methods of using "new" in C ++

Although there are three kinds of new usage, there are still two categories. Which two types are there? One is new operator, also called new expression; the other is operator new, also called new operator. These two English names are too difficult to confuse. Remember Chinese names. New expressions are common and most commonly used, for example:
String * ps = new string ("abc ");
The new expression above completes two tasks: applying for memory and initializing objects.
The new operator is similar to the malloc operator in C language and is only responsible for applying for memory. For example:
Void * buffer = operator new (sizeof (string ));
Note that an operator is added here. This is the second usage of new, which is quite common.
The third usage is not very common. The official statement is placement new, which is used to initialize objects in the given memory. That is to say, you have an idle memory in your hand. For example:
Void * buffer = operator new (sizeof (string ));
// Now the buffer is the pointer to your idle memory
Buffer = new (buffer) string ("abc"); // call placement new. initialize a string-type object in the memory to which the buffer points. The initial value is "abc"
In fact, placement new is also a new expression, but it has a parameter more than a common new expression. Of course, the operation and return values are different.
Therefore, the first use of new can be divided into two actions, which are the following two.

There are no three syntaxes for delete corresponding to new. There are only two syntaxes: delete operator and operator delete, also known as delete expressions and delete operators. The delete expression corresponds to the new expression to release the object structure and memory. The delete operator is only used for memory release, similar to the free operator in C language. For example:
String * ps = new string ("abc ");
Delete ps; // call the delete expression, which is parsed before being released
Void * buffer = operator new (sizeof (string ));
Operator delete (buffer); // release
Why is there no delete corresponding to placement new? Actually, there are. Placement new initializes the object at the specified position, that is, the constructor is called. Therefore, it corresponds to the destructor, but it is not called placement delete.
Void * pv = operator new (sizeof (vector <int> ));
Pv = new (pv) vector <int> (8, 0 );
Static_cast <vector <int> *> (pv)-> ~ Vector (); // call destruct function
Operator delete (pv); // free memory
Pv = NULL;

[Note] refer to more effective C ++

In addition, operator new will apply for a sizeof (int) memory to save the size of this space.

Placement new if you want to apply for more sizeof (INT) space to save the size of the array when applying for a data set. Eg,

Void * PTR = operatro new (sizeof (INT) * 100 );

Int * iptr = new (PTR) int [100]; // if this is the case, an error will occur here, because the system actually applied 100 + sizeof (INT) space! However, the PTR only has a size of 100 * szieof (INT), so the application space fails.

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: 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.