Overloads of the "go" C + + new operator

Source: Internet
Author: User

Basic concepts:

1. Operator Overloading: C + + supports assigning different semantics to an operator

2. New operator: request memory, call constructor

About overloads of the C + + new operator

Do you know the difference between the new operator of C + + and operator new? Maybe you'll ask, do they make a difference?

When you write the following code,

string New string ("memory managerment");

you are using the The new operator, like sizeof, is supported by the C + + language level. You can't change its semantics, it always does things the same: allocate enough memory to hold the object, and then call the constructor to initialize the memory allocated in the previous step. The New operator always does these two things, and you can't change its behavior.

All you can do is change the first step of the behavior, how to allocate RAW memory for the object . the operator new function is used to allocate raw memory for an object. The first step of the new operator is to call operator new. You can overload this function. Its prototype is as follows:

operator New (size_t size);

The return value of the function is void*, because this function returns a pointer. This pointer points to the raw, initialized memory. Its semantics are like malloc. In fact, it calls malloc internally. The parameter size specifies the amount of memory to allocate. You can add extra parameters when overloading, but the first parameter type must be size_t.

In most cases, you don't need to call operator new, in case you need to call it, the format of the call is:

operator New (sizeof(string));

function operator new Returns a pointer to a piece of memory sufficient to hold a string object.

Just like malloc,operator New's only responsibility is to allocate memory, which knows nothing about constructors. It is the work of the new operator to construct an uninitialized pointer returned by operator new as an object. When your compiler encounters the following code:

string New string ("memory managerment");

The pseudo-code class it generates is as follows:

void operator New (sizeof(stringstring::string("memory Managerment " ) on memory;string* Pa = static_cast<string*> (memory);

The second cloth contains a call to the constructor function. This is called by your compiler. So you're not too close to asking, can programmers call constructors manually? The answer is in the negative. But the compiler also gives you another compromise that you can achieve.

It is important to note that it does not make sense to call the constructor on an existing object. Because the constructor is used to initialize the object. But sometimes some memory is allocated but not initialized, and you need to construct an object in these memory. You can use a special version of the operator new function. A term called placement New's function to do this thing.

Back to the example of the previous string, we can use placement NEW:

operator New (sizeof(string)); Stringnewstring("memory managerment");

The above two sentences are equivalent to What the new operator has done.

This is the whole secret of operator new and placement new. In general, you do not need to overload and explicitly call these two functions.

Original address: Http://blog.csdn.net/bichenggui/article/details/4823978#comments

Related Article

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.