C ++ (copy and assign value functions) 10 from the perspective of Assembly

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

The copy constructor and the copy function are two important functions in the class. What is the difference between the two? In fact, it is also very simple. For example, we can add a class definition:

[CPP] View plaincopy
  1. ClassApple
  2. {
  3. Public:
  4. Apple () {printf ("Apple ()! \ N");}
  5. Apple (Apple & A) {printf ("Copy Apple ()! \ N");}
  6. Apple & operator = (Apple & A) {printf ("= Apple () \ n");Return*This;}
  7. ~ Apple () {printf ("~ Apple ()! \ N");}
  8. VoidPrint ()Const{Return;}
  9. };

So what are the called functions in the following functions?

[CPP] View plaincopy
    1. VoidProcess ()
    2. {
    3. Apple A, C;
    4. Apple B =;
    5. C = B;
    6. }

In fact, this is the result of the compilation. You can take a look at it and try to read it yourself. If you do not understand it at a time, you can read it several more times.

[CPP] View plaincopy
  1. 70: Apple A, C;
  2. 0040127d Lea ECx, [ebp-10h]
  3. 00401280 call @ ILT + 70 (Apple: Apple) (0040104b)
  4. 00401285 mov dword ptr [ebp-4], 0
  5. 0040128c Lea ECx, [ebp-14h]
  6. 0040128f call @ ILT + 70 (Apple: Apple) (0040104b)
  7. 00401294 mov byte PTR [ebp-4], 1
  8. 71: Apple B =;
  9. 00401298 Lea eax, [ebp-10h]
  10. 0040129b push eax
  11. 0040129c Lea ECx, [ebp-18h]
  12. 0040129f call @ ILT + 50 (Apple: Apple) (00401037)
  13. 004012a4 mov byte PTR [ebp-4], 2
  14. 72: c = B;
  15. 004012a8 Lea ECx, [ebp-18h]
  16. 004012ab push ECx
  17. 004012ac Lea ECx, [ebp-14h]
  18. 004012af call @ ILT + 75 (Apple: Operator =) (00401050)
  19. 73 :}
  20. 004012b4 mov byte PTR [ebp-4], 1
  21. 004012b8 Lea ECx, [ebp-18h]
  22. 004012bb call @ ILT + 0 (Apple ::~ Apple) (00401005)
  23. 004012c0 mov byte PTR [ebp-4], 0
  24. 004012c4 Lea ECx, [ebp-14h]
  25. 004012c7 call @ ILT + 0 (Apple ::~ Apple) (00401005)
  26. 004012cc mov dword ptr [ebp-4], 0 ffffffffh
  27. 004012d3 Lea ECx, [ebp-10h]
  28. 004012d6 call @ ILT + 0 (Apple ::~ Apple) (00401005)
  29. 004012db mov ECx, dword ptr [ebp-0Ch]
  30. 004012de mov dword ptr fs: [0], ECx
  31. 004012e5 pop EDI
  32. 004012e6 pop ESI
  33. 004012e7 pop EBX
  34. 004012e8 add ESP, 58 h
  35. 004012eb cmp ebp, ESP
  36. 004012ed call _ chkesp (004087c0)
  37. 004012f2 mov ESP, EBP
  38. 004012f4 pop EBP
  39. 004012f5 RET

CodeFor example, you can view the corresponding assembly code according to 70, 71, 72, and 73:

(1) 70 sentences: we can see that the function has been called twice, which happens to be Apple's constructor call. This also corresponds to two temporary variables A and C, the addresses of the two variables are [ebp-10] and [ebp-14], here we can see that the size of the entire class is 4 bytes, it is a common memory for storing data. The main reason why constructors can be bound with the corresponding memory is that ECx records the starting address of the memory, which is critical in C ++ compilation. We can see that the C ++ constructor seems to be not bound to the memory. In fact, the conventions have been made in VC. ECx is the this pointer, which is the class memory start address. If you are interested, let's take a look at the register that stores this pointer during G ++ compilation? (Actually eax)

(2) 71: The eax records the address of the referenced variable, and ECx is next to four bytes under EBP. However, the address of the function call is not the same as the default constructor, so we can guess that the constructor here is a copy constructor. We can check the Print message during debugging.

(3) 72 sentences: The 0x4012af statement clearly tells us that the called function here is the operator = function, which is the reload of arithmetic operators, we will focus on this in our blog later.

(4) 73: as we have mentioned earlier, the Destructor are automatically called at the end of the function call. So here we see three calls appear? These three variables are exactly the three variables A, B, and C we mentioned earlier. So what is the order of the three variable calls? Let's look at the address of the variable, which is [ebp-18h], [ebp-14h], [ebp-10h], which is exactly the opposite order of the variable. So we can see that the Destructor and the constructor correspond exactly to each other, who appears first and then analyzes the constructor.

 

 

[notice: the following blog summarizes some of the phenomena in the structure and analysis.]

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.