C ++ new

Source: Internet
Author: User

In C ++, new actually has three meanings:

1. New operator

2. Operator new

3. Placement new

Our most common usage is

A * A = new ();

Here, new is the new operator, which consists of three steps:

1. allocate memory

2. Call the constructor of.

3. Return memory pointer

The first step is to allocate memory, which is actually to use operator new. The effect is similar to that of malloc. If you call it directly, it is as follows:

A = Operator new (size); or

Operator new [] (size );

Step 2: Call the object constructor. The actual call is placement new. If you call the constructor directly, it is as follows:

New (a) ();

If you use placement new (that is, a is the pointer on the stack) for the pointer on the stack, you must explicitly call the destructor.

A-> ~ A (); this is the only situation where you need to explicitly call the destructor.

 

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.