C ++ resentment Note 1 -- data transmission process of class objects as function parameters
The complicated mechanism of C ++, the boring textbooks, and the unavoidable use of it are doomed to create a group of C ++ resentful people. As the first part of the C ++ resentment series, this article observes the data transfer process when class objects are used as function parameters from the perspective of compilation.
Unless otherwise specified, the compiler uses VC ++, And the disassembly uses Windbg. Below are their version numbers:
Microsoft (R) 32-bit C/C ++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Microsoft (R) Windows Debugger Version 6.11.0001.404 X86
The test code is as follows:
[Cpp]
Class Node
{
Public:
Node (){}
// Node (Node & n );
Int data1;
Int data2;
Int data3;
Int data4;
Int data5;
Int data6;
Int data7;
};
// Node: Node (Node & n)
//{
//}
Void Fn (int a, Node n, int B)
{
N. data1 = 100;
N. data2= 100;
A = 100;
B = 10;
}
Void main ()
{
Node n;
Fn (1, n, 2 );
}
--------------------------------------------------
When the copy constructor is not used, the Fn disassembly code is called:
[Plain]
00fa1421 6a02 push 2; third parameter into Stack
00fa1423 83ec1c sub esp, 1Ch; Allocate stack memory for Node n. Note that the constructor Node () is not called.
00fa1426 b907000000 mov ecx, 7; rep cycles
00fa142b 8d75e0 lea esi, [ebp-20h]; Node n address
00fa142e 8bfc mov edi, esp; stack space address
00fa1430 f3a5 rep movs dword ptr es: [edi], dword ptr [esi]; copy n content to stack space
; A5 MOVS m32, m32 Move doubleword
; At address DS :( E) SI to address ES :( E) DI
00fa1432 6a01 push 1; the first parameter is added to the stack.
00fa1434 e8a2fdffff call hello! ILT + 470 (? FnYAXHVNodeHZ) (00fa11db)
00fa1439 83c424 add esp, 24 h; restore stack balance, 4 + 1CH + 4 = 24 H
The class object parameter is located on the stack and is allocated by sub esp size. Data is initialized through memory copy.
--------------------------------------------------
When the copy constructor is used, the above Code removes the comment and calls the disassembly code of Fn:
[Plain]
01002406 6a02 push 2; third parameter into Stack
01002408 83ec1c sub esp, 1Ch; open up stack space
0100240b 8bcc mov ecx, esp; stack memory first address saved in ecx, copy this pointer of the constructor
0100240d 8d45e0 lea eax, [ebp-20h]; real parameter address
01002410 50 push eax; As a copy constructor Parameter
01002411 e8d4edffff call hello! ILT + 485 (?? 0NodeQAEAAV0Z) (010011ea); copy constructor, replace rep movs memory copy
01002416 6a01 push 1; first parameter into Stack
01002418 e8beedffff call hello! ILT + 470 (? FnYAXHVNodeHZ) (010011db)
0100241d 83c424 add esp, 24 h; restore stack balance
The class parameter is still on the stack and is also allocated through sub esp size. Data is initialized through the copy constructor, and the mechanism of C ++ is numerous -- |.
--------------------------------------------------
The following is the disassembly result of Fn. No matter how Node n is initialized, it is okay to locate its position on the stack.
[Plain]
Hello! Fn:
00a41a60 55 push ebp | old ebp | ebp
00a41a61 8bec mov ebp, esp | ------------- |
| Ret address | ebp + 4
00a41a63 81ecc0000000 sub esp, 0C0h | ------------- |
| Int a | ebp + 8
00a41a69 53 push ebx | ------------- |
00a41a6a 56 push esi | Node n | ebp + 0CH
00a41a6b 57 push edi | ------------- |
| Int B | ebp + 28 H
00a41a6c 8dbd40ffffff lea edi, [ebp-0C0h]
00a41a72 b930000000 mov ecx, 30 h
00a41a77 b8cccccccc mov eax, 0 CCCCCCCCh
00a41a7c f3ab rep stos dword ptr es: [edi]; used for local variable space initialization, unique to debug
00a41a7e c7450c64000000 mov dword ptr [ebp + 0Ch], 64 h; n. data1 = 100; show that ebp + 0Ch is the starting address of parameter n
00a41a85 c7451064000000 mov dword ptr [ebp + 10 h], 64 h; n. data2 = 100;
00a41a8c c7450864000000 mov dword ptr [ebp + 8], 64 h; a = 100;
00a41a93 c745280a000000 mov dword ptr [ebp + 28 h], 0Ah; B = 10;
00a41a9a 5f pop edi
00a41a9b 5e pop esi
00a41a9c 5b pop ebx
00a41a9d 8be5 mov esp, ebp
00a41a9f 5d pop ebp
00a41aa0 c3 ret
--------------------------------------------------
Summary:
When class objects are used as function parameters, they are stored on the stack and do not affect the data of real parameters.
If you do not override the copy constructor, other constructor of the class will not be called. The data of the parameter is transmitted through the memory copy. If it is overwritten, the copy constructor will be called at the initialization of the parameter and no memory copy will be performed.
By tms_li