More effective tive c ++ Note 2 ----- Item M8: new and delete for different meanings

Source: Internet
Author: User

I. What is mentioned above

1. new operator indicates that new is an operator.

2. operator new refers to the function called by the new operator during memory allocation. This function is used to ultimately complete memory allocation. The operator new function is usually declared as follows: void * operator new (size_t size). Note that a void * type is returned, but this function is somewhat different from a common function. It is an overloaded operator function, similar to bool Carrot :: operator = (const Carrot & var. operator reload point me

3. The action of new [] operator and operator new [] is the same as that above, except that the memory is allocated to the array.

 

Ii. Excerpt

When you write this code: string * ps = new string ("Memory Management ");

New is new operator. This operator is built into the language just like sizeof. You cannot change its meaning, and its functions are always the same. The functions to be completed are divided into two parts. The first part is to allocate enough memory to accommodate the required types of objects. The second part is that it calls the constructor to initialize objects in the memory. The new operator always does these two things, and you cannot change its behavior in any way. New operator calls a function to complete the necessary memory allocation. You can rewrite or reload this function to change its behavior. The new operator is named operator new for the function called for memory allocation.

Like malloc, operator new only allocates memory. It knows nothing about constructors. Operator new understands memory allocation. It is the work of the new operator to pass the unprocessed pointer returned by operator new to an object. When your compiler encounters such a statement:


String * ps = new string ("Memory Management ");
This code is more or less similar to the following code:

Void * memory = operator new (sizeof (string); // The unprocessed memory is a String object.
Call string: string ("Memory Management") // initializes an object in * memory Memory.
String * ps = static_cast <string *> (memory); // The ps Pointer Points to the new object.

Iii. Demo program:

// You can customize operator new and operator delete functions.
Void * operator new (size_t size)
{
Void * p = NULL;
P = malloc (size );
Return p;
}

Void operator delete (void * ptr)
{
Free (ptr); // memory released
}
// Customize the operator new [] and operator delete [] functions to implement the same functions as above
Void * operator new [] (size_t size)
{
Void * p = NULL;
P = malloc (size );
Return p;
}
Void operator delete [] (void * ptr)
{
// Memory release
Free (ptr );
}
Int _ tmain (int argc, _ TCHAR * argv [])
{

Char * buffer1 = new char [100]; // automatically calls operator new (100 ),
Delete [] buffer1; // automatically call operator delete (buffer2)


// The following code automatically calls the operator new (sizeof (type) function and enters the constructor of the string object.
// This also verifies the two fixed functions of new operator: automatically Calling operator new functions and object constructor.
// Delete operator not only releases the allocated memory, but also calls the destructor of the string object.
String * buffer2 = new string ("hello world ");
Delete buffer2;

// Call the operator new function directly. If there is a custom function, enter the customized operator new function.
Void * buffer3 = operator new (10 );
Operator delete (buffer3 );

// Digress
// 1. In addition to allocating 20 bytes of memory, the following code also enters a piece of assembly code to set all the memory content to 0.
// 1. It works the same as memset (buffer4,). I personally prefer this :)
Char * buffer4 = new char [20] ();
Delete [] buffer4;

Return 0;
}

 

Iii. Summary
To create an object on heap, you should use new operator, which allocates both memory and calls constructor for the object. If you only want to allocate memory, you should call the operator new function. It will not call the constructor. If you want to customize your memory allocation process when the heap object is created, you should write your own operator new function, and then use the new operator. The new operator will call your customized operator new (the meaning of this sentence is: if you have customized your own operator new function (global), when we use new operator, the system will automatically call your custom operator new function to allocate memory, otherwise, the default operator new function will be called. If you want to create an object in a memory with obtained pointers, you should use placement new (for more information about placement new, see the original article ).

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.