C ++ resentment Note 2 -- function return value is a Class Object

Source: Internet
Author: User
C ++ resentment Note 2 -- function return value is a Class Object

To avoid redundant code, the program uses release to configure and compile, but the/OD option must be specified. Otherwise, the compiler optimization will be difficult to understand.
When the return value of a function is a basic data type (such as int, char), the returned result is placed on eax, so that the function caller can obtain the function return result through eax. But what if the returned value is a class object? Eax is not enough.

Experiment source code:

class Node{public:    Node(){}    //Node(Node& n){}    int data1;    int data2;    int data3;};Node Fn(){    Node n;    n.data1 = 100;        return n;}void main(){    Fn();}

----------------------------------------------------------------------
The caller's processing process. Main allocates a temporary space for FN on the stack, and the size of the returned object is the size of the FN. If the code is not optimized, one for FN appears and two for FN appears, and so on. You can copy "FN ();" several times to try. When FN is called, the address of the temporary space allocated for FN is pushed to the stack, and this address is used internally by FN to store the returned results.

Hello! Main: 012a1040 55 push ebp012a1041 8bec mov EBP, esp012a1043 83ec0c sub ESP, 0ch; open up temporary space, sizeof (node) size is 12, namely 0ch012a1046 8d45f4 Lea eax, [ebp-0Ch] 012a1049 50 push eax; press temporary space address 012a104a e8c1ffffff call Hello! FN (012a1010) 012a104f 83c404 add ESP, 4012a1052 33c0 XOR eax, eax012a1054 8be5 mov ESP, ebp012a1056 5d pop EBP

----------------------------------------------------------------------
The disassembly result of FN when no constructor is copied. FN copies the result to the temporary space, and the address of the temporary space has been pushed into the stack when FN is called by main.

Hello! FN [E: \ Hello \ hello. CPP @ 29]: 29 012a1010 55 push ebp29 012a1011 8bec mov EBP, esp29 012a1013 83ec0c sub ESP, 0ch; allocated memory 30 012a1016 8d4df4 Lea ECx, [ebp-0Ch]; this pointer of the non-argument constructor is 30 012a1019 e8e2ffffff call Hello! Node: node (012a1000); 31 012a101e c745f464000000 mov dword ptr [ebp-0Ch], 64 h; N. data1 = 100; 33 012a1025 8bda-8 mov eax, dword ptr [EBP + 8]; temporary space address 33 012a1028 8b4df4 mov ECx, dword ptr [ebp-0Ch] when calling FN; copy the first member data1_012a102b 8908 mov dword ptr [eax], ecx33 012a102d 8b55f8 mov edX, dword ptr [ebp-8]; copy the second member data233 012a1030 895004 mov dword ptr [eax + 4], edx33 012a1033 8b4dfc mov ECx, dword ptr [ebp-4]; copy data3, if many 33 012a1036 894808 mov dword ptr [eax + 8] and ECx are members, rep movs will be used to copy 33 012a1039 8b1_8 mov eax, dword ptr [EBP + 8] 34 012a103c 8be5 mov ESP, ebp34 012a103e 5d pop ebp34 012a103f C3 RET

----------------------------------------------------------------------

After the copy constructor is added (after the annotation is removed), FN submits the copy task to the copy constructor and the temporary space address to the constructor. Therefore, the called copy constructor will affect the temporary space allocated in main, so as to realize data transmission, that is, the return of class objects.

Hello! FN: 01381020 55 push ebp01381021 8bec mov EBP, esp01381023 83ec0c sub ESP, 0ch; allocated memory 01381026 8d4df4 Lea ECx, [ebp-0Ch] 01381029 e8d2ffffff call Hello! Node: node (01381000); constructor 0138102e c745f464000000 mov dword ptr [ebp-0Ch], 64 h; N. data1 = 100; 01381035 8d45f4 Lea eax, [ebp-0Ch] 01381038 50 push eax01381039 8b4d08 mov ECx, dword ptr [EBP + 8]; temporary space address, as this pointer 0138103c e8cfffff call Hello! Node: node (01381010); call the copy constructor 01381041 8bda-8 mov eax, dword ptr [EBP + 8] 01381044 8be5 mov ESP, ebp01381046 5d pop ebp01381047 C3 RET

Related Article

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.