虚拟表在内存中的布局?

发布于 2024-08-02 22:14:24 字数 224 浏览 9 评论 0原文

虚拟表如何存储在内存中?他们的布局?

例如,

class A{
    public:
         virtual void doSomeWork();
};

class B : public A{
    public:
         virtual void doSomeWork();
};

A类和B类的虚拟表在内存中的布局如何?

how are virtual tables stored in memory? their layout?

e.g.

class A{
    public:
         virtual void doSomeWork();
};

class B : public A{
    public:
         virtual void doSomeWork();
};

How will be the layout of virtual tables of class A and class B in memory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

故事未完 2024-08-09 22:14:24

对于 Linux 中的 GCC 编译器,运行:

g++ -fdump-lang-class example.h

注意:在 GCC 8 之前,选项为 -fdump-class-hierarchy 而不是 -fdump-lang-class

输出是:

Vtable for A
A::_ZTV1A: 3u entries
0     (int (*)(...))0
8     (int (*)(...))(& _ZTI1A)
16    (int (*)(...))A::doSomeWork

Class A
   size=8 align=8
   base size=8 base align=8
A (0x7fb76785a4e0) 0 nearly-empty
    vptr=((& A::_ZTV1A) + 16u)

Vtable for B
B::_ZTV1B: 3u entries
0     (int (*)(...))0
8     (int (*)(...))(& _ZTI1B)
16    (int (*)(...))B::doSomeWork

Class B
   size=8 align=8
   base size=8 base align=8
B (0x7fb7678510d0) 0 nearly-empty
    vptr=((& B::_ZTV1B) + 16u)
  A (0x7fb76785a540) 0 nearly-empty
      primary-for B (0x7fb7678510d0)

我还创建了 vtable-dumper 工具来列出虚拟表的内容在共享对象中。使用此工具,您不需要编译标头,只需在对象上运行它:

vtable-dumper SHLIB

For GCC compiler in Linux run:

g++ -fdump-lang-class example.h

NOTE: Before GCC 8, the option is -fdump-class-hierarchy instead of -fdump-lang-class.

The output is:

Vtable for A
A::_ZTV1A: 3u entries
0     (int (*)(...))0
8     (int (*)(...))(& _ZTI1A)
16    (int (*)(...))A::doSomeWork

Class A
   size=8 align=8
   base size=8 base align=8
A (0x7fb76785a4e0) 0 nearly-empty
    vptr=((& A::_ZTV1A) + 16u)

Vtable for B
B::_ZTV1B: 3u entries
0     (int (*)(...))0
8     (int (*)(...))(& _ZTI1B)
16    (int (*)(...))B::doSomeWork

Class B
   size=8 align=8
   base size=8 base align=8
B (0x7fb7678510d0) 0 nearly-empty
    vptr=((& B::_ZTV1B) + 16u)
  A (0x7fb76785a540) 0 nearly-empty
      primary-for B (0x7fb7678510d0)

Also I've created the vtable-dumper tool to list contents of virtual tables in the shared objects. With this tool you don't need to compile headers, just run it on the object:

vtable-dumper SHLIB
病女 2024-08-09 22:14:24

正如其他人所说,这取决于编译器,而不是您在日常使用 C++ 时真正需要考虑的事情。但是,如果您只是对这个问题感到好奇,您应该阅读 Stan Lippman 的书 C++ 对象模型内部

As others have said, this is compiler dependant, and not something that you ever really need to think about in day-to-day use of C++. However, if you are simply curious about the issue, you should read Stan Lippman's book Inside the C++ Object Model.

十雾 2024-08-09 22:14:24

内存中的 vtable 布局完全依赖于编译器;没有采取“正确”或通用的方法。

vtable layout in memory is completely compiler dependent; there's no "correct" or universal approach taken.

漫雪独思 2024-08-09 22:14:24

正如其他人已经写过的那样,没有通用的方法。 (哎呀,甚至没有人强制要求使用虚拟表。)

但是,我相信它们很可能被实现为引用函数指针表的对象中某个偏移处的隐藏指针。某些虚拟函数的地址占用该表中的某些偏移量。通常还有一个指向动态类型的 std::type_info 对象的指针。

如果您对此类内容感兴趣,请阅读 Lippmann 的“C++ 对象模型内部”< /a>.然而,除非您的兴趣是学术性的(或者您正在尝试编写一个 C++ 编译器——但您不需要询问),否则您不应该打扰。这是您不需要了解也不应该依赖的实现细节。

As others already wrote, there is no general approach. (Heck, nobody even mandates that virtual tables are used at all.)

However, I believe they are most likely implemented as a hidden pointer at a certain offset in the object which references a table of function pointers. Certain virtual functions' addresses occupy certain offsets in that table. Usually there's also a pointer to the dynamic type's std::type_info object.

If you're interested in things like this, read Lippmann's "Inside the C++ Object Model". However, unless your interest is academic (or you're trying to write a C++ compiler -- but then you shouldn't need to ask), you shouldn't bother. It's an implementation detail you don't need to know and should never rely on.

情深缘浅 2024-08-09 22:14:24

来自 维基百科

C++ 标准没有强制要求
动态调度到底应该是怎样的
已实施

所以答案是否定的。 vtable 的布局是实现定义的。

From wikipedia:

The C++ standards do not mandate
exactly how dynamic dispatch must be
implemented

So the answer is no. Layout of vtable is implementation defined.

御守 2024-08-09 22:14:24

有关 Open Watcom 类布局的详细说明,请查看 类布局笔记

For a very detailed description of Open Watcom's class layout have a look at the Class Layout notes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文