gpt4 book ai didi

c++ - 内存中的虚拟表布局?

转载 作者:可可西里 更新时间:2023-11-01 16:51:39 32 4
gpt4 key购买 nike

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

例如

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

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

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

最佳答案

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

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

输出是:

Vtable for AA::_ZTV1A: 3u entries0     (int (*)(...))08     (int (*)(...))(& _ZTI1A)16    (int (*)(...))A::doSomeWorkClass A   size=8 align=8   base size=8 base align=8A (0x7fb76785a4e0) 0 nearly-empty    vptr=((& A::_ZTV1A) + 16u)Vtable for BB::_ZTV1B: 3u entries0     (int (*)(...))08     (int (*)(...))(& _ZTI1B)16    (int (*)(...))B::doSomeWorkClass B   size=8 align=8   base size=8 base align=8B (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

关于c++ - 内存中的虚拟表布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1342126/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com