虚拟表和内存布局在多个虚拟继承中 [英] Virtual tables and memory layout in multiple virtual inheritance

查看:146
本文介绍了虚拟表和内存布局在多个虚拟继承中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的层次结构:

  struct A {
int a;
A(){f(0); }
A(int i){f(i); }
virtual void f(int i){cout<一世; }
};
struct B1:virtual A {
int b1;
B1(int i):A(i){f(i); }
virtual void f(int i){cout< i + 10; }
};
struct B2:virtual A {
int b2;
B2(int i):A(i){f(i); }
virtual void f(int i){cout< i + 20; }
};
struct C:B1,virtual B2 {
int c;
C():B1(6),B2(3),A(1){}
virtual void f(int i){cout< i + 30; }
};




  1. 布局 C 实例?它包含多少个vptrs,它们在哪里放置?哪些虚拟表与C的虚拟表共享?每个虚拟表包含什么?



    这里我如何理解布局:

      -------------------------------------------------- ---------- 
    | vptr1 | AptrOfB1 | b1 | B2ptr | c | vptr2 | AptrOfB2 | b2 | a |
    ---------------------------------------------- ------------------

    其中 AptrOfBx 是指向 A 的实例, Bx 继承是虚拟的)。

    是正确的吗?哪些函数 vptr1 指向?给定以下代码


    $

    b $ b

      C * c = new C(); 
    dynamic_cast< B1 *>(c) - > f(3);
    static_cast< B2 *>(c) - > f(3);
    reinterpret_cast< B2 *>(c) - > f(3);

    为什么所有调用 f code> 33 ?



解决方案>虚拟基础与普通基础非常不同。请记住,虚拟是指在运行时确定 - 因此整个基本子对象必须在运行时确定。



想象你获得 B& x 引用,并且您的任务是找到 A :: a 成员。如果继承是真实的,那么 B 有一个超类 A ,因此 / code> - 您通过 x 查看的对象有一个 A 您的成员 A :: a 。如果 x 的最多派生对象具有 A 类型的多个基址,则只能看到该特定副本是 B 的子对象。



但是如果继承是虚拟的,这些都没有意义。我们不知道哪个 A -subobject我们需要 - 这个信息在编译时不会存在 。我们可以处理一个实际的 B - 对象,如 B y; B& x = y; ,或者使用 C - 像 C z; B& x = z; ,或者完全不同的东西,实际上从 A 派生更多次。唯一的方法是在运行时查找实际库 A



这可以用一个更多级别的运行时间接来实现。 (注意这是如何完全平行于如何虚拟函数与非虚函数相比,使用一个额外的运行时间间隔来实现。)一个解决方案是不具有指向vtable或基本子对象的指针以将指针存储到指向实际基本子对象的指针。这有时被称为thunk或trampoline。



因此,实际对象 C z; 如下。

  +  -  + ---内存中的实际排序取决于编译器和不重要的--- ++  -  + ------ ++ ----- ++ ----- + 
| T | B1 || T | B2 || C || A |
+ - + ------ ++ - + ------ ++ ----- ++ ----- +
| | |
V V ^
| | + -Thunk- + |
+ --->> ---- + - >> --- | - >> - +
+ ------- +



,无论你是否有 B1& B2& ,你都先查找thunk,反过来告诉你在哪里找到实际的基本子对象。这也解释了为什么你不能从 A& 到任何派生类型执行静态转换:这些信息在编译时根本不存在。



有关更详细的说明,请参阅这篇文章。 (在该描述中,thunk是 C 的vtable的一部分,并且虚拟继承总是需要vtables的维护,即使没有虚拟 em>任何地方。)


Consider following hierarchy:

struct A {
   int a; 
   A() { f(0); }
   A(int i) { f(i); }
   virtual void f(int i) { cout << i; }
};
struct B1 : virtual A {
   int b1;
   B1(int i) : A(i) { f(i); }
   virtual void f(int i) { cout << i+10; }
};
struct B2 : virtual A {
   int b2;
   B2(int i) : A(i) { f(i); }
   virtual void f(int i) { cout << i+20; }
};
struct C : B1, virtual B2 {
   int c;
   C() : B1(6),B2(3),A(1){}
   virtual void f(int i) { cout << i+30; }
};

  1. What's the exact memory layout of C instance? How many vptrs it contains, where exactly each of them is placed? Which of virtual tables are shared with virtual table of C? What exactly each virtual table contains?

    Here how I understand the layout:

    ----------------------------------------------------------------
    |vptr1 | AptrOfB1 | b1 | B2ptr | c | vptr2 | AptrOfB2 | b2 | a |
    ----------------------------------------------------------------
    

    where AptrOfBx is the pointer to A instance that Bx contains (since the inheritance is virtual).
    Is that correct? Which functions vptr1 points to? Which functions vptr2 points to?

  2. Given the following code

    C* c = new C();
    dynamic_cast<B1*>(c)->f(3);
    static_cast<B2*>(c)->f(3);
    reinterpret_cast<B2*>(c)->f(3);
    

    Why all the calls to f print 33?

解决方案

Virtual bases are very different from ordinary bases. Remember that "virtual" means "determined at runtime" -- thus the entire base subobject must be determined at runtime.

Imagine that you are getting a B & x reference, and you are tasked to find the A::a member. If the inheritance were real, then B has a superclass A, and thus the B-object which you are viewing through x has an A-subobject in which you can locate your member A::a. If the most-derived object of x has multiple bases of type A, then you can only see that particular copy which is the subobject of B.

But if the inheritance is virtual, none of this makes sense. We don't know which A-subobject we need -- this information simply doesn't exist at compile time. We could be dealing with an actual B-object as in B y; B & x = y;, or with a C-object like C z; B & x = z;, or something entirely different that derives virtually from A many more times. The only way to know is to find the actual base A at runtime.

This can be implemented with one more level of runtime indirection. (Note how this is entirely parallel to how virtual functions are implemented with one extra level of runtime indirection compared to non-virtual functions.) Instead of having a pointer to a vtable or base subobject, one solution is to store a pointer to a pointer to the actual base subobject. This is sometimes called a "thunk" or "trampoline".

So the actual object C z; may look as follows. The actual ordering in memory is up to the compiler and unimportant, and I've suppressed vtables.

+-+------++-+------++-----++-----+
|T|  B1  ||T|  B2  ||  C  ||  A  |
+-+------++-+------++-----++-----+
 |         |                 |
 V         V                 ^
 |         |       +-Thunk-+ |
 +--->>----+-->>---|     ->>-+
                   +-------+

Thus, no matter whether you have a B1& or a B2&, you first look up the thunk, and that one in turn tells you where to find the actual base subobject. This also explains why you cannot perform a static cast from an A& to any of the derived types: this information simply doesn't exist at compile time.

For a more in-depth explanation, take a look at this fine article. (In that description, the thunk is part of the vtable of C, and virtual inheritance always necessitates the maintenance of vtables, even if there are no virtual functions anywhere.)

这篇关于虚拟表和内存布局在多个虚拟继承中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆