New/delete and new[]/delete[]

Source: Internet
Author: User

New/delete and new[]/delete[]

This article takes the following base as an example, debugging and validating, new and delete, and new[] and delete[] are all standard library functions, by overloading the four functions above.

1classBase{
2 Public:
3Base(void){
4printf"base\n");
5}
6
7Staticvoid*operatorNew(size_t CB,intNblockuse,ConstChar* szFileName,intNLine) {
8printf"operator new\n");
9return::operator New(CB);
Ten}
One
AStaticvoid*operatorNew[] (size_t CB,intNblockuse,ConstChar* szFileName,intNLine) {
-printf"operator new[]\n");
-return::operatorNew(CB);
the}
-
-voidoperatorDelete(void* p,size_t) {
-printf"operator delete\n");
+}
-
+voidoperatorDelete[](void* p,size_t) {
Aprintf"operator delete[]\n");
at}
-
-~Base(void){
-printf"~base\n");
-}
-};

1 new and Delete expressions 1.1 new-expression base* PB = new base;

When you execute a new expression, there are actually three steps:

    • First , call the standard library function named operator new (which can be overloaded), and its function is to allocate a large enough amount of the original type of memory to hold the object of the specified type;
    • then , run a constructor of that type , constructing the object with the specified initialization;
    • finally , the pointer to the newly assigned and constructed object is returned .

The above statement outputs:

operator New
Base

1.2 Delete expression Delete pb;

When you execute a delete expression, there are actually two steps:

    • First , run the appropriate destructor on the object pointed to by the pointer;
    • The memory used by the object is then freed by calling a standard library function named operator new (which can be overloaded).

The above statement outputs:

~ Base
operator Delete

2 new[] and delete[] Expressions 2.1 new[] Expressions

Base* PB = New Base[ 3];

When you execute a new[] expression, there are actually three steps:

    • First, Call a standard library function (which can be overloaded) named operator new[] . Its function is to allocate a large enough amount of the original type of memory and to save the size of the created array at the lowest address with four bytes .
    • then, by the size of the above array, run multiple constructors of that type ;
    • Finally, The return array is a pointer to an array object, not the starting address of all allocated object spaces.

The above statement outputs:

operator New[]
Base
Base
Base

2.2 delete[] Expression delete[] p;

Execute delete[] expression, there are actually two steps:

    • first , the size of the array is obtained based on the 4 byte space of the lowest address of the array, and then several destructors are called ;
    • then , call the operator delete[] standard library function (which can be overloaded) to release all memory for the object.

The above statement outputs:

operator New[]
Base
Base
Base

Note: The compiler looks for the dimensions of the array only when the brackets appear, otherwise it assumes that only a single objects is to be deleted.

3 Special Occurrences

The correct use of the pairing is new and delete, and new[] and delete[] in pairs, so as to ensure that the correct release of memory space and the correct invocation of destructors, but sometimes they are not the correct pairing between them, the following cases

3.1 New and delete[] Error pairing Base* PB = new base;
Delete [] PB;

Output:

operator New
Base
~ Base
operator Delete

From the above results: when new an object is disposed with delete[], this is used with normal new and delete pairs, the output is the same, that is, the disposed object.

3.2 new[] and delete error pairing base* p = new base[3];
Delete p;

Output:

operator New[]
Base
Base
Base
~ Base
operator Delete

From the above results: When multiple objects are created using new[], but the first object is freed only when the object is disposed with delete. Other elements still exist-although their associated memory has been requested to be returned.

New/delete and new[]/delete[]

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.