A detailed explanation of the time for the destructor of temporary objects in C + + _c language

Source: Internet
Author: User

In C + +, the temporary object calls the destructor as soon as it is not needed, releasing the resource it occupies, while the named object invokes the destructor in turn, in reverse order of creation.

C + + Source:

Copy Code code as follows:

Class X {
Public
int i;
Int J;
~x () {}
X () {}

};

int main () {
X X1;
X ();
x1.i = 1;
X x2;


}


corresponding assembly code:
Copy Code code as follows:

_main PROC

; 11:int Main () {

Push EBP
MOV EBP, esp
Sub ESP, 24; Reserve 24byte space for X1 temporary Object x2

; 12:X X1;

Lea ECX, DWORD PTR _X1$[EBP]; Gets the first address of the X1 object, passing in the constructor as a suppressed parameter
Call?? 0x@ @QAE @xz; Calling constructors for X1

; 13:x ();

Lea ECX, DWORD PTR $T 2559[EBP]; Gets the first address of the temporary object, passing in the constructor as an implied parameter
Call?? 0x@ @QAE @xz; Calling constructors for temporary objects
Lea ECX, DWORD PTR $T 2559[EBP]; Gets the first address of the temporary object, passing in the destructor as a suppressed parameter
Call?? 1x@ @QAE @xz; Calling a destructor for a temporary object

; 14:x1.i = 1;

mov DWORD PTR _x1$[ebp], 1; write 1 to the first address of X1 memory, 1 to the member variable i in X1

; 15:x x2;

Lea ECX, DWORD PTR _X2$[EBP]; Gets the first address of the X2, passing in the constructor as an implied parameter
Call?? 0x@ @QAE @xz; Calling constructors for X2

; 16:
; 17:
; 18:}

Lea ECX, DWORD PTR _X2$[EBP]; Gets the first address of the X2, as the implied parameter passes in the destructor
Call?? 1x@ @QAE @xz; Calling destructors for X2
Lea ECX, DWORD PTR _X1$[EBP]; Gets the first address of the X1, as the implied parameter passes in the destructor
Call?? 1x@ @QAE @xz; Calling destructors for X1
xor eax, EAX
mov esp, EBP
Pop EBP
RET 0
_main ENDP


As you can see from the assembly code above, a temporary object does call a destructor after it is not needed, although it is created before the X2 object, but is still refactored before the X2 object. The X1 x2 destructors Call order, in contrast to the order in which their constructors are called.

And look at the following:

The source code in C + +:

Copy Code code as follows:

Class X {
Public
int i;
Int J;
int k;
X () {}
~x () {}
};


int main () {
X X1;
X (), x1.i = 1;//here is a comma operator
X x2;
}


Here, after the temporary object is modified, there is a comma expression, not a semicolon.

The following is the remit code:

Copy Code code as follows:

; 12:int Main () {

Push EBP
MOV EBP, esp
Sub ESP, 36; Reserve 36 bytes of space for X1 temporary Object x2

; 13:X X1;

Lea ECX, DWORD PTR _X1$[EBP]; Gets the first address of the X1, passed to the constructor as a suppressed parameter
Call?? 0x@ @QAE @xz; Calling constructors for X1

; 14:x (), x1.i = 1;//here is a comma operator

Lea ECX, DWORD PTR $T 2560[EBP]; Gets the first address of the temporary object, passed to the constructor as a suppressed parameter
Call?? 0x@ @QAE @xz; Calling constructors for temporary objects
mov DWORD PTR _x1$[ebp], 1; Assigns 1 to the memory at the X1 's first address, which is the member variable I assigned to X1 1
Lea ECX, DWORD PTR $T 2560[EBP]; Gets the first address of the temporary variable passed to the destructor as a suppressed parameter
Call?? 1x@ @QAE @xz; Calling a destructor for a temporary object

; 15:x x2;

Lea ECX, DWORD PTR _X2$[EBP]; Gets the first address of X2, passed to the constructor as a suppressed parameter
Call?? 0x@ @QAE @xz; Calling constructors for X2

; 16:}

Lea ECX, DWORD PTR _X2$[EBP]; Gets the first address of the X2, passed to the destructor as an implied parameter
Call?? 1x@ @QAE @xz; Calling destructors for X2
Lea ECX, DWORD PTR _X1$[EBP]; Gets the first address of the X1, passed to the destructor as an implied parameter
Call?? 1x@ @QAE @xz; Calling destructors for X1
xor eax, EAX
mov esp, EBP
Pop EBP
RET 0
_main ENDP


As you can see, the first difference is that, after the temporary object is constructed, the destructor is not invoked immediately after the destructor is called, but after the assignment statement after the comma is executed.

Sum up:

The time a temporary object calls a destructor is when a high-level language finishes execution, and a high-level language completes with a semicolon. Therefore, the time when a temporary object calls a destructor is when it encounters a semicolon

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.