Call of member functions for C ++ object layout and polymorphism implementation

Source: Internet
Author: User

Starting from this part, we will not only use memory information printing for exploration, but also track and observe the compilation code generated by the compiler to understand how the compiler implements these language features. The Assembly knowledge is beyond the scope of this article. I only parse the assembly code discussed with us. Understanding the knowledge to be discussed in this article does not require a complete compilation knowledge, but you must understand the minimum concept.

Next, let's take a look at the impact of virtual inheritance. To make a comparison, Let's first look at the call of common member functions.

Run the following code:

C010 obj;

PRINT_OBJ_ADR(obj)

obj.foo();

C012::sfoo();

C010 * pt = &obj;

pt-> foo();

The result is as follows:

objs address is : 0012F843

This is the memory address of the obj object.

First, let's look at the object's common member function call, obj. foo ();, the corresponding assembly code is:

00422E09 lea ecx, [ebp + FFFFF967h]

00422E0F call 0041E289

Row 3 stores the object address in the ecx register. After executing this command, we will see that the value in ecx is 0x0012F843, Which is the value printed above. If the function needs to pass parameters, we will see some push commands in front. In row 3, we can see that the call is a direct address, which is static binding. That is, the call address of the function has been determined by the compiler during compilation.

After tracking, we want to see that it is a jump command. If we continue to execute the command, we can see the real function code section as follows (Note: I added a line number before the first line for convenience of discussion ):

  01 00425FE0 push ebp

  02 00425FE1 mov ebp,esp

  03 00425FE3 sub esp,0CCh

  04 00425FE9 push ebx

  05 00425FEA push esi

  06 00425FEB push edi

  7 00425FEC push ecx

  08 00425FED lea edi,[ebp+FFFFFF34h]

  09 00425FF3 mov ecx,33h

  10 00425FF8 mov eax,0CCCCCCCCh

  11 00425FFD rep stos dword ptr [edi]

  12 00425FFF pop ecx

  13 00426000 mov dword ptr [ebp-8],ecx

  14 00426003 mov eax,dword ptr [ebp-8]

  15 00426006 mov byte ptr [eax],2

  16 00426009 pop edi

  17 0042600A pop esi

  18 0042600B pop ebx

  9 0042600C mov esp,ebp

  20 0042600E pop ebp

  21 0042600F ret

Let's take a look at Row 3 and import the ecx Register into the stack. the last four lines initialize the part of the function stack that saves local variables. The value of ecx is displayed in row 12th. The value of ecx is the memory address of the object stored before the function call. Row 13th stores the value of this pointer as a local variable. In this way, we know that VC7.1 does not pass this pointer through the pressure stack as it passes through common functions, but through the ecx register. Rows 14th and 15 use this pointer to assign values to the member variables of the object.

Let's look at the assembly code for calling static member functions:

     00422E14 call 0041DD84

It is very direct because it does not need to process this pointer and traces the assembly code of the function. We can see that this pointer does not need to be processed. The specific code is not listed here.

Let's take a look at calling the common member function pt-> foo (); through a pointer. The resulting assembly code is as follows:

   00422E25 mov ecx,dword ptr [ebp+FFFFF958h]

   00422E2B call 0041E289

It is similar to the code used to call common member functions through objects. However, when the object address is stored in the ecx register, the address of the object is found by referencing the pt pointer.

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.