Reload the C ++ new operator

Source: Internet
Author: User

Reload the C ++ new operator

 

Address: http://blog.csdn.net/bichenggui/article/details/4823978

 

Do you know the difference between the new operator of C ++ and operator new? Maybe you will ask, are they different?

When you write the following code,

String * pA = new string ("memory managerment ");

You are using the new operator. This operator is the same as sizeof and is supported at the C ++ language level. You cannot change its semantics. What it does remains unchanged: 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 cannot change its behavior.

All you can do is change the first step. How to allocate raw memory to objects. The operator new function is used to allocate original memory to objects. The first step of the new operator is to call operator new. You can reload this function. Its prototype is as follows:

Void * operator new (size_t size );

The Return Value of the function is void *, because the function returns a pointer. This Pointer Points to the native memory for initialization. Its semantics is like malloc. Actually, it calls malloc internally. The size parameter specifies the size of the memory to be allocated. You can add additional parameters when reloading, but the first parameter type must be size_t.

In most cases, you do not need to call operator new. In case you need to call operator new, the calling format is as follows:

Void * rawmemory = Operator new (sizeof (string ));

The operator new function returns a pointer pointing to a memory sufficient to accommodate a String object.

Just like malloc, operator new's only responsibility is to allocate memory, which is ignorant of constructors. Constructing an uninitialized pointer returned by operator new into an object is the work of the new operator. When your compiler encounters the following code:

String * pA = new string ("memory managerment ");

Its pseudo-code class is as follows:

Void * Memory = Operator new (sizeof (string ));

Call string: string ("memory managerment") on memory;

String * pA = static_cast <string *> (memory );

The second section contains the call of the constructor. This is called by your compiler. So you may ask, can programmers manually call constructor? The answer is no. But the compiler also provides you with another compromise that you can achieve.

It must be noted that calling constructor on an existing object makes no sense. Because constructors are used to initialize objects. But sometimes some memory has been allocated but not initialized, You need to construct an object in the memory. You can use a special version of operator new function. A function called Placement new is used to do this.

Return to the previous string example, we can use placement new as follows:

Void * Memory = Operator new (sizeof (string ));

String * pA = new (memory) string ("memory managerment ");

The above two sentences are equivalent to what the new operator does.

This is all the secrets of operator new and placement new. Generally, you do not need to overload or explicitly call these two functions.

 

Address: http://blog.csdn.net/bichenggui/article/details/4823978

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.