To: C ++'s new

Source: Internet
Author: User

The new in C ++ is actually a very confusing term. It has two different meanings: the new operator and the new function. It is worth record.

A new operator

The most common operator is the new operator, for example:

String * STR = new string ("test new ");

As an operator, like sizeof, new is built-in C ++. You cannot make any changes to it except using it.

New allocates a block of memory on the stack and automatically calls the class constructor.

2. New Function

The second type is the new function. In fact, the memory allocated by the new operator is the new function. The prototype is:

Void * operator new (size_t size );

The new function returns a void pointer, an uninitialized memory. As you can see, this is similar to the malloc action in C. You can reload the new function and add additional parameters, but you must ensure that the first parameter must be of the size_t type, it specifies the size of the allocated memory block. c ++ allows you to do so. Of course, this is not necessary in general. If the new function is reloaded, the new function is called when the new operator is used.

If you use the new function, the code relative to the statement string * STR = new string ("test new") is roughly as follows:

String * STR = (string *) operator new (sizeof (string ));

Str. String ("test new"); // Of course, this call is invalid, but the compiler does not have this restriction.

This is not complete yet, and there is a third new.

Three placement new

Third, placement new, which is also a usage of New as a function. It allows you to allocate an object on an existing memory, the data in the memory will not be overwritten or overwritten by you. Placement new is also called by the new operator. The call format is:

New (buffer) type (size_t size );

Let's take a look at the following code:

Char STR [22];

Int DATA = 123;

Int * pA = new (& Data) int;

Int * pb = new (STR) int (9 );

The result is * pA = 123 (the original data is not overwritten), and * pb = 9 (the original data is overwritten). We can see that placement new is not allocated with new memory, you can also use the memory allocated on the stack, not limited to the heap.

To use placement new, you must include <New> or <New. h>

In fact, the placement new is the same as the second one, but the parameter is added, which is a heavy load of the function new. The syntax format is:

Void * operator new (size_t, void * buffer );

It may look like this:

Void * operator new (size_t, void * buffer) {return buffer ;}

 

Delete corresponds to new, and memory needs to be recycled. Otherwise, it will be leaked. Write it again next time and recall the contents of today.

Summary

1. function new

Void * operator new (size_t size); allocate a block of memory on the heap, and placement new (void * operator new (size_t, void * buffer )); create an object on an existing memory. If you already have a memory, placement new is very useful. In fact, it is widely used in STL.

2. Operator new

There is nothing to say about the most commonly used new.

3. The new function does not automatically call the class constructor because it does not know the allocated memory type. The new operator automatically calls the class constructor.

4. The new function allows overloading, while the new operator cannot.

5. The corresponding Delete is followed.

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.