Why does C + + define a destructor for a class of operator new[] 4 bytes of incoming arguments?

Source: Internet
Author: User

Questions:On the Internet to see people write a code like this:
1 classA2 {  3  Public:  4 A ()5     {  6std::cout<<"Call A Constructor"<<Std::endl; 7     }  8   9~A ()Ten     {   Onestd::cout<<"Call A destructor"<<Std::endl;  A     }   -    -     void*operator New(size_t size) the     {   -std::cout<<"Call a::operator new[] Size:"<<size<<Std::endl;  -         returnmalloc (size);  -     }   +     void operatorDelete[] (void*p) -     {   +std::cout<<"Call a::operator delete[]"<<Std::endl;  A Free (p);  at     }    -     void operatorDeletevoid*p) -     {   - Free (p);  -     }    -}; #include <iostream> #include"A.h"   in    - void*operator New[] (size_t size) to {   +std::cout<<"Call Global new[] Size:"<<size<<Std::endl;  -     returnmalloc (size);  the }   *    $ void operatorDelete[] (void*p)Panax Notoginseng {   -std::cout<<"Call Global delete[]"<<Std::endl;  the }   + int_tmain (intARGC, _tchar*argv[]) A {   thestd::cout<<"sizeof A"<<sizeof(A) <<Std::endl;  +A * p1 =Newa[3];  - delete []p1;  $     $System"Pause");  -     return 0;  -}
If you define a destructor:
Operator new[] will output:
Call
Global new[] Size:7
Otherwise output:
Call
Global new[] Size:3
Answer:1. Why is there a destructor 4 bytes more
This extra space is meant to record the length of the array and the memory alignment.
Let's look at the statement in the C + + standard. It is mentioned here that delete does not know how long the array is. This duty is done by new[]. The implementation of new[] in the standard library is generally to apply for a piece of sizeof (T) * n + x space, using the initial spatial record array length, starting from the next aligned address is the actual space used by the object array. This can be observed in the following simple modification procedures. The new[] function prints and returns the value of malloc before printing the this pointer in the constructor.
How much of this extra X-space is specific, this C + + standard is not strictly defined, just give some conditions. The main idea is to ensure that the actual space occupied by the object must conform to the memory alignment requirements of the current platform. For x86, it is 4-byte alignment, that is, the pointer must be divisible by 4 as an integer. For x64, it is 8-byte aligned. Combined with the above paragraph new[] should be responsible for recording the length of the array, the following rules can be drawn.
1. The initial space in the space returned by new[] is used for the length record, and a 4 byte int is used on the main measured VC.
2. Space based on memory alignment requirements.
3. The actual space occupied by the object array.
The whole situation is this: [Size][pad][array]
How to test our conclusions?
Add the following code between new and delete in the main function:
1         sizeof (void1; 2         1 ); 3         P2 = p2 & ~mask; 4         Std::cout << *reinterpret_cast<int*> (p2) << Std::endl;
You can get the size of the array. 2. When no destructor is in progress
In 1 we mentioned new[] the space requested can be greater than the actual space required by the array to record the array length, but in the absence of an effective destructor (non-trivial destructor, non-standard translation) The delete simply frees up the contiguous memory space. So there is no need to log the array length, at which point the space requested by new[] can be equal to the actual space required by the array.
As for the effective destructor, it is simply that both itself and its non-static members (including inherited) must not have a destructor defined or deleted. Applying an array of such an object generally does not use extra space to record its length. http://www.zhihu.com/question/26765276

Why does C + + define a destructor for a class of operator new[] 4 bytes of incoming arguments?

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.