More effective C + +: different new and delete

Source: Internet
Author: User

People sometimes seem to like to deliberately make C + + language terminology difficult to understand. For example, the difference between the new operator (new operator) and operator new.

When you write this code:

string *ps = new string("Memory Management");

The new you are using is the new operator. This operator, like sizeof, is built into the language, and you can't change its meaning, it's always the same function. The functionality it has to complete is divided into two parts. The first part is the object that allocates enough memory to hold the desired type. The second part is that it calls the constructor to initialize the object in memory. The new operator always does both things, and you can't change its behavior in any way.

What you can change is how to allocate memory for an object. The new operator calls a function to complete the necessary memory allocation, and you can override or overload the function to change its behavior. The new operator is the name of the function called operator new for allocating memory.

function operator new usually declares this way:

void * operator new(size_t size);

The return value type is void*, because this function returns an unprocessed (raw) pointer to the uninitialized memory. (If you like, you can write a operator new function that can initialize memory to store values before returning a pointer, but generally not.) Parameter size_t determine how much memory is allocated. You can add additional parameters to the overloaded function operator new, but the first parameter type must be size_t. (For more information about operator new See effective C + + clause 8 to clause 10.) )

You don't normally call operator new, but once you do, you can call it just like you call other functions:

void *rawMemory = operator new(sizeof(string));

Operator operator new returns a pointer to a piece of memory that is sufficient to hold a string object.

Just like malloc, operator New's job is to allocate memory. It knows nothing about constructors. Operator new is about memory allocation. Passing the unhandled pointer returned by operator new to an object is the work of the new operator. When your compiler encounters such a statement:

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.