Resolves new, operator new, operator new[] and delete, operator delete, operator delete[in C + +

Source: Internet
Author: User

Note: The following tests are performed under VS2015, and other compilers may be slightly different.

because the content will be more, so split into the top and bottom two pieces to write.

Recently in a base project to do the demand, in the base code to see a large number of overloaded operator New/delete of the wheel code, and this is not too much to pay attention to, so take the time to carefully check the data, wrote the demo code, and finally the C + + new/ Delete the members of this family have an understanding.

To see, about new and delete can be divided into two major groups:

One, new/delete and its corresponding array version new[]/delete[].

Second, the so-called operator New/delete (which contains a named placement operator new, it is in C + + has already provided a good operator overloaded version), and its corresponding array version new[]/delete[].

Let's look at the following.

one, New/delete, and corresponding array versions new[]/delete[].

These two groups can be considered as one, and we are the first in C + +, they are predefined in C + +, we can not overload them, their use is to request and release heap memory, for example:

coperatortest* pop2_1 = new Coperatortest ();
Other stuff
Delete pop2_1;

Here, New/delete actually do two things separately:

For New:1. Application memory (via operator new), size is sizeof (coperatortest). 2. Call the Coperatortest constructor (call a different version of the constructor if you return an angry pass).

For Delete:1. Call the Coperatortest destructor. 2. Free memory (via operator delete).

coperatortest* Poparr = new Coperatortest[3];
Other stuff
delete[] Poparr;
Here's new[]/delete[] also do two things separately:

For New[]:1. Request the memory space (through operator new[]), depending on the size of the array, and record the size (used in deletep[]). 2. Constructs the required number of objects, which is the constructor that invokes the number of array sizes.

For Delete[]:1. Calls the destructor of the corresponding number of times, based on the size of the previously stored array. 2. Free space (via operator delete[]).
Here are a few actual demos:

Declare and define the following classes to overload the operator New,operator New[],operator Delete and operator delete[ in the class respectively (about operator XX This family will be explained in detail later, Here for demo New/delete only):

//coperatortest.h class Coperatortest {public:coperatortest ();

    Virtual ~coperatortest ();
    void* operator new (size_t size);

    void operator Delete (void* p);
    void* operator new[] (size_t size);
void operator delete[] (void* p); };
COperatorTest.cpp
#include "OperatorTest.h"

coperatortest::coperatortest (void)
{
    std::cout << "Coperatortest ()" << Std::endl;
}

Coperatortest::~coperatortest (void)
{
    std::cout << "~coperatortest ()" << Std::endl;
}

void* coperatortest::operator New (size_t size)
{
    std::cout << "Coperatortest::operator New with Size:" << size << Std::endl;
    return malloc (size);
}

void Coperatortest::operator Delete (void* p)
{
    std::cout << "coperatortest::operator Delete" << Std::endl;
    Free (p);
}

void* coperatortest::operator new[] (size_t size)
{
    std::cout << "Coperatortest::operator new[] With Size: "<< size << Std::endl;
    return malloc (size);
}

void Coperatortest::operator delete[] (void* p)
{
    std::cout << "Coperatortest::operator delete[]" < < Std::endl;
    Free (p);
}
Make the following call:

int main ()
{
    //coperatortest::operator new
    coperatortest* pop1_0 = new Coperatortest ();
    Std::cout << Std::endl;

    Coperatortest::operator Delete
    Delete pop1_0;
    Std::cout << Std::endl;
    Std::cout << Std::endl;

    //coperatortest::operator new[]
    coperatortest* poparr1_0 = new Coperatortest[3];
    Std::cout << Std::endl;

    Coperatortest::operator delete[]
    Delete [] poparr1_0;
	
    return 0;
}
The output results are as follows:

The results coincide with the above analysis:

1. Call new, first call operator new allocation space, the size of the class size, here is 4 (because there are virtual functions: destructors, there will be a virtual table pointer), the delete behavior is also consistent with the above description.

2. Call new[], first call operator new[] Allocate space, (note that the space size is 16, that is, the size of the array * class size + 4, here is 3 * 4 + 4, the extra four bytes are used to store the array size so that the array can be used when it is released), and then call 3 times the class constructor. Delete[], calls the 3 destructor and then calls operator delete[] to free up space.

It also involves calling this class or calling global operator XX, and we continue to overload the Global version of these four operator and make calls ( Note that they are overloaded global):

void* operator new (size_t size) {std::cout << "global operator New with size:" << size << Std::endl
    ;
return malloc (size);
    } void operator Delete (void* p) {std::cout << "global operator delete" << Std::endl;
Free (p); } void* operator new[] (size_t size) {std::cout << "global operator new[] with size:" << size << St
    D::endl;
return malloc (size);
    } void operator delete[] (void* p) {std::cout << "global operator delete[]" << Std::endl;
Free (p);
    int main () {//global operator new coperatortest* pop1_1 =:: New Coperatortest ();

    Std::cout << Std::endl;
    Global operator Delete::d elete pop1_1;
    Std::cout << Std::endl;

    Std::cout << Std::endl;
    Global operator new[] coperatortest* poparr1_1 =:: New coperatortest[3];
	
    Std::cout << Std::endl;
    Global operator delete[]::d elete[] poparr1_1;
return 0; }
The results of the operation are as follows:


To see, to invoke the global version, you need to precede with two colons (::), order of execution, size, and so on, and so on with the version of the class part, and emphasize that the overloaded operator in the class coperatortest will be invoked without a double colon call.

Several conclusions relating to the invocation of New/delete and new[]/delete[] operator XX Precedence:

1. The user-overloaded version of the priority invocation parameter blending, whether global or class-wide.

2. The overloaded version in the class takes precedence over the version of the global overload being invoked.

3. In the case where the overloaded version of the parameter blending exists in the class, the operator XX (new coperatortest) that the class itself overloads is invoked preferentially, noting that coperatortest::new coperatortest () cannot be written.

4. In the case where the overloaded version of the parameter blending exists in the class, only the display call (preceded by a two colon) will invoke the overloaded version of Operator XX (:: New Coperatortest ()) of the global blending.

5. If any operator xx is not overloaded in the class, the global version is invoked according to rule 1 (the existence of a blending global overloaded version is invoked, otherwise the default global version of C + + is invoked), regardless of whether the call is preceded by a two colon.

6. If there are overloaded versions in the class, but no parameters are completely blending, without a two-colon invocation of the premise, will not continue to look out the global version of the error, the compile error, hint parameters do not match, plus two colon calls, according to rule 1 call the global version (there is a blending user-defined global version, then called, Otherwise invoke the C + + default global version).

The example above shows the 1,2,3,4 entry, and the rest of you can test it yourself.


When testing the call to the global version of operator new[]/delete[], it is found that if the version of blending is not present, but there is a blending non-array global version operator New/delete, the non-array global version is invoked because, when there is no matching array version, Takes a non-array version and is then implemented on each of the array elements.


This is the content of New/delete and the corresponding array version new[]/delete[], and the next one will sort out the contents of operator New/delete and its corresponding array version new[]/delete[].


The next article is complete, please refer to.

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.