C + + object layout and invocation of member functions for polymorphic implementations

Source: Internet
Author: User

Starting from this section, in addition to using the memory of the information printing to explore, more by tracking and observing the compiler generated by the assembly code to understand the compiler's implementation of these language features. The discussion of assembly knowledge is beyond the scope of this article, and I only parse the assembly code that is relevant to our discussion. Understanding the knowledge discussed in this article does not require a complete compilation of knowledge, but must understand the minimum concept.

Let's look at the effects of introducing virtual inheritance. For comparison, let's look at the invocation of ordinary member functions first.

Executes the following code, which includes a normal member function call to an object, a static member function call to a class, and a normal member function called by a pointer:

C010 obj;
PRINT_OBJ_ADR(obj)
obj.foo();
C012::sfoo();
C010 * pt = &obj;
pt->foo();

The results are as follows:

Obj ' s address is:0012f843

This is the memory address of the Obj object.

First we look at the ordinary member function call of the object, Obj.foo (), and the corresponding assembly code is:

00422E09 Lea ecx,[ebp+fffff967h]

00422e0f Call 0041E289

Line 1th The address of the object into the ECX register, after the execution of this line of instructions, we want to see the ecx in the value of 0x0012f843, is the previous printed value. If the function needs to pass arguments, we'll see some push instructions in the front. In line 2nd we can see that the call is a direct address, which is the static binding. That is, the call address of the function has been compiler-resolved at compile time.

We're going to go in there. We want to see that is a jump instruction, continue to perform can see the real function Code section, as follows (note: For the sake of discussion, I added a line number before line):

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
07 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
19 0042600C mov esp,ebp
20 0042600E pop ebp
21 0042600F ret

Let's look at line 7th, put the ECX register into the stack, and the next 4 lines initialize the part of the function's stack that holds the local variable. Line 12th pops up the ECX value, where the ECX value remains the object memory address that was deposited before the function call, and line 13th is the value that holds the this pointer as a local variable. So we know that VC7.1 is not passing the this pointer through the stack like passing a normal function, but through the ECX register. Line 14th and 15 Use this this pointer to assign values to the member variables of the object.

Then look at the assembly code for the static member function call:

00422E14 Call 0041dd84

Very straightforward, because it does not need to process the this pointer, trace to the assembly code of the function, and you can see that the same does not need to process this pointer. The specific code is not listed here.

Then look at the normal member function Pt->foo () through the pointer, and the resulting assembly code is as follows:

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

00422E2B Call 0041E289

Similar to the code that invokes ordinary member functions through an object. But the object address to the ECX register is to find the object address by means of a reference to the PT pointer.

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.