C ++ (pointer 2) 03.2 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]

 

(4) pointer and reference

References are one of the differences between C ++ and languages. In essence, the two are the same. Let's take a look at the following two paragraphs.Code.

A) function code for pointers and pointers

[CPP] View plaincopy
    1. VoidAdd_point (Int* Q)
    2. {
    3. (* Q) ++;
    4. }
    5. VoidAdd_ref (Int& Q)
    6. {
    7. Q ++;
    8. }

B) function call code

[CPP] View plaincopy
  1. 56:IntM = 10;
  2. 004012e8 mov dword ptr [ebp-4], 0ah
  3. 57: add_point (& M );
  4. 004012ef Lea eax, [ebp-4]
  5. 004012f2 push eax
  6. 004012f3 call @ ILT + 45 (process) (00401032)
  7. 004012f8 add ESP, 4
  8. 58: add_ref (m );
  9. 004012fb Lea ECx, [ebp-4]
  10. 004012fe push ECx
  11. 004012ff call @ ILT + 50 (add_ref) (00401037)
  12. 00401304 add ESP, 4
  13. 59:Return1;
  14. 00401307 mov eax, 1
  15. 60 :}

After analysis, we find that the functions implemented by the add_point function and the add_ref function both perform auto-increment processing on the input data. Only when processing, the input parameter of a function is a needle, and the input parameter of a function is a reference. When calling a function, you can find that the pointer and reference are the same. First, let's take a look at add_point. The first sentence gets the address of M and copies it to eax. The second sentence is stack processing, the third sentence is the function add_point, and the fourth sentence is the stack backtracking. Let's also take a look at add_ref. The first sentence gets the address of M and copies it to ECx. The second sentence is ECx pressure stack processing. The third sentence calls the function add_ref and the fourth sentence is stack backtracking. I believe that here, we will understand why C ++'s predecessors encouraged you to use more references.

(5) pointer and struct

When learning data nodes, I believe everyone has learned such a data structure definition:

[CPP] View plaincopy
    1. Typedef Struct_ Node
    2. {
    3. IntData;
    4. Struct_ Node * next;
    5. } Node;

At that time, we did not understand what this struct meant? In fact, this definition is completely changed to the following:

[CPP] View plaincopy
    1. Typedef Struct_ Node
    2. {
    3. IntData;
    4. Void* Next;
    5. } Node;

The two data structures are actually completely consistent. The first data stores the data, the second data is a pointer, And the content is a data type address. The identified address type is the same as that of the void * type. However, the previous one is more direct. The latter type of address is convenient, but it is troublesome to convert each time it is used. If you are interested, take a look at the following question:

[CPP] View plaincopy
    1. Typedef Struct_ Node
    2. {
    3. Struct_ Node * next;
    4. } Node;

Can we regard the node address as node * or node *? Is there a difference between the two? (Actually there is no difference)

In the Linux kernel code, there is a method for calculating the offset value. For details, refer:

[CPP] View plaincopy
    1. IntOffset = (Int) & (Node *) (0)-> next );

(6) Class pointer

The class pointer is complicated, but you can see some clues from a small example:

[CPP] View plaincopy
  1. ClassFruit
  2. {
  3. Public:
  4. Fruit (){}
  5. ~ Fruit (){}
  6. VoidPrint () {printf ("Fruit! \ N");}
  7. };
  8. ClassApple:PublicFruit
  9. {
  10. Public:
  11. Apple (){}
  12. ~ Apple (){}
  13. VoidPrint () {printf ("Apple! \ N");}
  14. };
  15. VoidProcess ()
  16. {
  17. Fruit F;
  18. Apple * A = (Apple *) & F;
  19. A-> Print ();
  20. Fruit * B = & F;
  21. B-> Print ();
  22. }

If you are familiar with C ++, you will soon know the answer to this question. Why does a and B point to the same address and use the same print function, but the result is different. I think this is mainly because the data types of the two are different. Once a pointer is bound to a certain data type, all the behavior of this pointer is actually limited by this type of data. The general data type is the same as the custom class type. As long as the print function is not preceded by virtual, we will find that both print calls are hard-coded, which is similar to common function calls. Therefore, pointers cannot be separated from data types. Leaving a specific type of address makes no sense. Just like void * can be forgiven, but void is unacceptable. The assembly code below demonstrates this point.

[CPP] View plaincopy
  1. 65: fruit F;
  2. 0040132d Lea ECx, [ebp-10h]
  3. 00401330 call @ ILT + 35 (fruit: Fruit) (00401028)
  4. 00401335 mov dword ptr [ebp-4], 0
  5. 66: Apple * A = (Apple *) & F;
  6. 0040133c Lea eax, [ebp-10h]
  7. 0040133f mov dword ptr [ebp-14h], eax
  8. 67: A-> Print ();
  9. 00401342 mov ECx, dword ptr [ebp-14h]
  10. 00401345 call @ ILT + 0 (Apple: print) (00401005)
  11. 68: Fruit * B = & F;
  12. 0040134a Lea ECx, [ebp-10h]
  13. 0040134d mov dword ptr [ebp-18h], ECx
  14. 69: B-> Print ();
  15. 00401350 mov ECx, dword ptr [ebp-18h]
  16. 00401353 call @ ILT + 25 (fruit: print) (0040101e)

 

(Full text)

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.