Object layout and vtables (Itanium C++ ABI)
The standard specifies only a few rules about the memory layout of class objects and leaves the rest to the ABI; this page uses Clang's layout and vtable dumps to show how data members, empty bases, vptrs, vtables, thunks and virtual bases are laid out and work under the Itanium ABI.
Key points
- The standard only specifies that later-declared members have higher addresses, that the first member of a standard-layout class has the same address as the object, and that an object occupies at least one byte (except zero-sized subobjects). How much padding, where the vptr goes and how virtual functions are dispatched are decided by the ABI.
- Empty bases and
[[no_unique_address]]members can take no space, but two objects of the same type can't share an address. - The Itanium ABI reuses tail padding only for non-POD classes, where POD follows the C++03 definition, which corresponds exactly to no term in today's standard.
- A polymorphic class has a vptr at offset 0, pointing into the vtable just past offset_to_top and RTTI; a virtual call is two loads plus an indirect jump.
- Multiple inheritance relies on secondary vtables and thunks to adjust
this; virtual inheritance relies on vbase_offset, vcall_offset, construction vtables and the VTT to locate virtual bases at run time.
What the standard specifies#
The standard says very little about what class objects look like in memory, and that is deliberate. It specifies only the following:
- Non-bit-field non-static data members of non-zero size declared later have higher addresses ([expr.rel]/4.2; a note in [class.mem.general]/24 restates this). There may be padding between members, and space needed to manage virtual functions and virtual bases.
- The address of an object of a standard-layout class equals the address of its first non-static data member, and of each of its base class subobjects ([class.mem.general]/31). The conditions for standard-layout classes are listed in [class.prop]/2, for example no virtual functions or virtual bases, and the same access control for all non-static data members.
- Every object (except bit-fields and zero-sized subobjects) occupies at least one byte. Two objects with overlapping lifetimes can have the same address only if one is nested within the other, or if one of them is a zero-sized subobject ([intro.object]/8, [intro.object]/10).
Beyond that, how much padding goes between members, where the vptr goes, where base class subobjects are placed and how virtual functions are dispatched are all left to the implementation. For code generated by different compilers to call each other, they must agree on these questions, and that agreement is the ABI (application binary interface). This page covers the Itanium C++ ABI used by GCC and Clang on Linux, macOS and similar platforms.
The layout and vtable output on this page are all generated by Clang 23:
clang++ -std=c++2c --target=x86_64-linux-gnu \
-Xclang -fdump-record-layouts -Xclang -fdump-vtable-layouts -c x.cpp -o /dev/nullThese are options of the compiler frontend (-cc1) and must be forwarded with -Xclang. Only classes that are actually used (whose size is needed or for which code must be generated) are printed, so the examples often use sizeof or function definitions to trigger them.
Data members and padding#
struct Padded { char c; int i; char d; };
struct Reordered { int i; char c; char d; }; 0 | struct Padded
0 | char c
4 | int i
8 | char d
| [sizeof=12, dsize=12, align=4,
| nvsize=12, nvalign=4]
0 | struct Reordered
0 | int i
4 | char c
5 | char d
| [sizeof=8, dsize=8, align=4,
| nvsize=8, nvalign=4]The rules are the same as in C. Each member is placed at the next offset satisfying its alignment requirement ([basic.align]); the class's alignment is the largest alignment among its members; and sizeof is rounded up to a multiple of the alignment, so that every element of an array is aligned.
Padded has only 6 bytes of data but takes 12, and reordering the declarations saves 4. The compiler won't reorder for you, because [expr.rel]/4.2 requires later-declared members to have higher addresses.
Access control no longer permits reordering
Before C++23, the standard only required members with the same access control to be in declaration order, and the relative order of different access-control sections was unspecified. P1847R4 removed this freedom: all non-static data members are now in declaration order. The mainstream ABIs never used that freedom anyway.
The other quantities in the output are Itanium ABI terms:
| Quantity | Meaning |
|---|---|
sizeof |
Size of a complete object |
dsize (data size) |
Size without tail padding, i.e. the position just past the last meaningful byte |
nvsize / nvalign |
Size and alignment excluding virtual bases, i.e. what the class occupies when embedded as a base class subobject |
The difference between dsize and sizeof decides whether tail padding can be reused; see below.
Empty classes and the empty base optimization#
struct Empty {};
struct EBO : Empty { int x; };
struct NoEBO { Empty e; int x; };
struct NUA { [[no_unique_address]] Empty e; int x; };
struct TwoEmpty : Empty { Empty e; int x; }; 0 | struct Empty (empty)
| [sizeof=1, dsize=1, align=1,
0 | struct EBO
0 | struct Empty (base) (empty)
0 | int x
| [sizeof=4, dsize=4, align=4,
0 | struct NoEBO
0 | struct Empty e (empty)
4 | int x
| [sizeof=8, dsize=8, align=4,
0 | struct NUA
0 | struct Empty e (empty)
0 | int x
| [sizeof=4, dsize=4, align=4,
0 | struct TwoEmpty
0 | struct Empty (base) (empty)
1 | struct Empty e (empty)
4 | int x
| [sizeof=8, dsize=8, align=4,Empty's sizeof is 1, because a complete object must have non-zero size ([intro.object]/8); otherwise the two elements of Empty a[2] would share an address.
Base class subobjects and members with [[no_unique_address]] are potentially-overlapping subobjects ([intro.object]/7), which may be zero-sized. For a standard-layout base class with no non-static data members, [intro.object]/8 says outright that it is zero-sized. So the Empty base of EBO and x are both at offset 0, and sizeof(EBO) == sizeof(int). This is the empty base optimization (EBO).
NoEBO's ordinary data member e isn't a potentially-overlapping subobject and must take at least one byte, which with int's alignment adds 4 bytes. NUA uses [[no_unique_address]] ([dcl.attr.nouniqueaddr]) to make the member a potentially-overlapping subobject too, with the same effect as EBO. Before C++20, standard library implementations made heavy use of private inheritance to borrow EBO, so that allocators, deleters and comparators would take no space; now a member with this attribute does the job directly.
TwoEmpty's base Empty and member e have the same type. By [intro.object]/10, two different objects of the same type can't share an address, so e is pushed to offset 1. That is why item 2.7 of [class.prop]/2 says that if a base class type may appear at offset 0 of the class (for example, the same type as the first member), the class isn't standard-layout, since its first member can't share an address with the base.
MSVC
According to public sources (not verified on our test machine), MSVC by default applies EBO only to the first empty base under multiple inheritance; __declspec(empty_bases) is needed for it to apply to all empty bases. MSVC ignores [[no_unique_address]] to preserve ABI compatibility, and provides [[msvc::no_unique_address]] instead.
Tail padding reuse#
In struct D : Base { char d; }, can d go into the padding at the end of Base? The Itanium ABI's answer depends on whether Base is a POD for the purpose of layout, a concept that follows the C++03 definition of POD. A C++03 POD class must be an aggregate, and so can have no user-declared constructors and no private or protected non-static data members.
struct PodBase { int i; char c; }; // POD in the C++03 sense
struct NonPodBase { int i; char c; NonPodBase() {} }; // user-declared constructor, not a POD
struct PrivBase { int i; private: char c; }; // private data member, not a POD
struct D1 : PodBase { char d; };
struct D2 : NonPodBase { char d; };
struct D3 : PrivBase { char d; }; 0 | struct D1
0 | struct PodBase (base)
0 | int i
4 | char c
8 | char d
| [sizeof=12, dsize=9, align=4,
0 | struct D2
0 | struct NonPodBase (base)
0 | int i
4 | char c
5 | char d
| [sizeof=8, dsize=6, align=4,
0 | struct D3
0 | struct PrivBase (base)
0 | int i
4 | char c
5 | char d
| [sizeof=8, dsize=6, align=4,PodBase's dsize is 8: its 3 bytes of tail padding count as data and can't be reused, so D1 can only put d at offset 8, for a total size of 12. NonPodBase and PrivBase have dsize 5, so the derived class puts d at offset 5 and the total size stays 8.
A POD's tail padding can't be reused because C and C++98 code does memcpy(&base, &other, sizeof(Base)) on PODs. If a derived class's member lived in the base's tail padding, that copy would overwrite it. For non-POD classes, the ABI assumes nobody copies base class subobjects that way. By today's standard, the guarantee that copying out a trivially copyable type's bytes and copying them back preserves the value explicitly excludes potentially-overlapping subobjects ([basic.types.trivial]/3), so such a memcpy on a base class subobject was never guaranteed anyway.
POD for the purpose of layout isn't a term of the current standard
PrivBase is trivially copyable, but because it has a private data member it is neither a C++03 POD nor a standard-layout class. Conversely, giving PodBase a user constructor that does nothing leaves it standard-layout, but it is no longer a C++03 POD. None of std::is_trivial, std::is_standard_layout or the deprecated std::is_pod accurately predicts whether the Itanium ABI will reuse tail padding. Changing one member's access control or adding an empty constructor can change the size of derived classes; that is an ABI change.
The same rule applies to [[no_unique_address]] members:
struct M1 { [[no_unique_address]] PodBase p; char d; };
struct M2 { [[no_unique_address]] NonPodBase p; char d; };
struct M3 { NonPodBase p; char d; }; 0 | struct M1
0 | struct PodBase p
...
8 | char d
| [sizeof=12, dsize=12, align=4,
0 | struct M2
0 | struct NonPodBase p
...
5 | char d
| [sizeof=8, dsize=6, align=4,
0 | struct M3
0 | struct NonPodBase p
...
8 | char d
| [sizeof=12, dsize=9, align=4,M2::d is at offset 5, reusing p's tail padding; the ordinary member M3::d is at offset 8. M1's p has the attribute, but PodBase's dsize is 8 to begin with, so d is at offset 8 too.
Polymorphic classes: vptr and vtable#
struct Base {
virtual ~Base();
virtual int f();
virtual int g();
int b = 1;
};
struct Derived : Base {
int f() override;
virtual int h();
int d = 2;
};
Base::~Base() {}
int Base::f() { return 1; }
int Base::g() { return 2; }
int Derived::f() { return 3; }
int Derived::h() { return 4; } 0 | struct Base
0 | (Base vtable pointer)
8 | int b
| [sizeof=16, dsize=12, align=8,
| nvsize=12, nvalign=8]
0 | struct Derived
0 | struct Base (primary base)
0 | (Base vtable pointer)
8 | int b
12 | int d
| [sizeof=16, dsize=16, align=8,
| nvsize=16, nvalign=8]A class with virtual functions has, at the start of the object, a pointer to a virtual function table (vtable), called the vptr. A polymorphic class isn't standard-layout (item 2.2 of [class.prop]/2), so the guarantee that the object's address equals its first member's address doesn't apply, and b is at offset 8.
Derived shares a single vptr with Base. Base is Derived's primary base, the first non-virtual dynamic base class; it is placed at offset 0, and the derived class extends directly after its vtable. Derived::d is placed at offset 12, reusing Base's tail padding, since a polymorphic class isn't a POD.
The structure of a vtable#
Vtable for 'Base' (6 entries).
0 | offset_to_top (0)
1 | Base RTTI
-- (Base, 0) vtable address --
2 | Base::~Base() [complete]
3 | Base::~Base() [deleting]
4 | int Base::f()
5 | int Base::g()
Vtable for 'Derived' (7 entries).
0 | offset_to_top (0)
1 | Derived RTTI
-- (Base, 0) vtable address --
-- (Derived, 0) vtable address --
2 | Derived::~Derived() [complete]
3 | Derived::~Derived() [deleting]
4 | int Derived::f()
5 | int Base::g()
6 | int Derived::h()| Slot | Content |
|---|---|
| offset_to_top | Offset from the subobject containing this vptr to the start of the complete object, here 0. dynamic_cast<void*> uses it to find the most derived object ([expr.dynamic.cast]) |
| RTTI | Points to the class's type_info object (_ZTI…), used by typeid and dynamic_cast |
| vtable address | The vptr actually points here, not at the start of the vtable |
| Virtual function pointers | In declaration order; a derived class's overriders replace the corresponding slots, and new virtual functions are appended at the end |
A virtual destructor takes two slots: [complete] only destroys, corresponding to D1 in the mangled name; [deleting] destroys and then calls operator delete, corresponding to D0 (see Name mangling (Itanium C++ ABI)). delete p dispatches through D0, which is how it can call the operator delete in the scope of the most derived class ([expr.delete], [class.free]).
In an object file, a vtable is just an array of pointers whose contents are filled in by relocations. Below is Derived's vtable (starting at 0x40) in v1.o compiled at -O0:
$ llvm-readelf -r v1.o # the .rela.data.rel.ro section, excerpt
0000000000000048 R_X86_64_64 _ZTI7Derived + 0
0000000000000050 R_X86_64_64 _ZN7DerivedD2Ev + 0
0000000000000058 R_X86_64_64 _ZN7DerivedD0Ev + 0
0000000000000060 R_X86_64_64 _ZN7Derived1fEv + 0
0000000000000068 R_X86_64_64 _ZN4Base1gEv + 0
0000000000000070 R_X86_64_64 _ZN7Derived1hEv + 0The offset_to_top at 0x40 is the constant 0 and needs no relocation. The complete destructor's slot holds D2: without virtual bases, D1 and D2 are the same, and Clang refers to D2 directly.
Constructors write the vptr, virtual calls read it#
Derived* make() { return new Derived; }
int call_g(Base& b) { return b.g(); }
void* most_derived(Base* p) { return dynamic_cast<void*>(p); }_Z4makev:
pushq %rax
movl $16, %edi
callq _Znwm@PLT
movabsq $8589934593, %rcx # 0x200000001: b = 1, d = 2 written at once
movq %rcx, 8(%rax)
leaq _ZTV7Derived+16(%rip), %rcx # vptr = vtable + 16, skipping offset_to_top and RTTI
movq %rcx, (%rax)
popq %rcx
retq
_Z6call_gR4Base:
movq (%rdi), %rax # load the vptr
jmpq *24(%rax) # 3rd slot after the vptr: g
_Z12most_derivedP4Base:
testq %rdi, %rdi
je .LBB7_1 # a null pointer gives a null pointer
movq %rdi, %rax
movq (%rdi), %rcx
addq -16(%rcx), %rax # p + offset_to_top
retq
.LBB7_1:
xorl %eax, %eax
retqThis is -O2 output. g is in the 3rd slot after the vptr (D1, D0, f, g in order), at offset 24, so a virtual call is two loads plus an indirect jump. dynamic_cast<void*> reads the offset_to_top two slots before the vptr.
RTTI objects#
_ZTI7Derived is itself a polymorphic object. Its vptr points to the vtable of some type_info subclass in the C++ runtime library: classes without bases use __cxxabiv1::__class_type_info, classes with exactly one non-virtual public base use __si_class_type_info, and everything else (multiple inheritance, virtual inheritance) uses __vmi_class_type_info. The three examples on this page refer to each of these. __dynamic_cast, the run-time implementation of dynamic_cast, searches along the base class information these objects record.
Multiple inheritance and thunks#
struct A { virtual int fa(); int a = 1; };
struct B { virtual int fb(); int b = 2; };
struct C : A, B { int fb() override; int c = 3; };
int C::fb() { return c; }
int call_fb(B& b) { return b.fb(); }
B* to_b(C* p) { return p; } 0 | struct C
0 | struct A (primary base)
0 | (A vtable pointer)
8 | int a
16 | struct B (base)
16 | (B vtable pointer)
24 | int b
28 | int c
| [sizeof=32, dsize=32, align=8,
Vtable for 'C' (7 entries).
0 | offset_to_top (0)
1 | C RTTI
-- (A, 0) vtable address --
-- (C, 0) vtable address --
2 | int A::fa()
3 | int C::fb()
4 | offset_to_top (-16)
5 | C RTTI
-- (B, 16) vtable address --
6 | int C::fb()
[this adjustment: -16 non-virtual] method: int B::fb()C has two vptrs: one in the A subobject (the primary base, shared with C), and one in the B subobject at offset 16. C's vtable is really two tables joined together: the primary vtable (slots 0–3) and the secondary vtable for B-in-C (slots 4–6). The secondary vtable's offset_to_top is −16, because getting from the B subobject back to the start of C means subtracting 16.
call_fb only knows it has a B&, and passes the pointer to the B subobject as this to the function in the vtable slot. But C::fb expects this to point at the start of C. So slot 6 of the secondary vtable holds not C::fb itself but a thunk: a small piece of code that subtracts 16 from this and then runs C::fb.
_ZN1C2fbEv: # C::fb()
movl 0x1c(%rdi), %eax # this->c, c is at offset 28 in C
retq
_ZThn16_N1C2fbEv: # non-virtual thunk to C::fb()
movl 0xc(%rdi), %eax # this points to the B subobject, c is 12 bytes after it
retq
_Z4to_bP1C:
leaq 0x10(%rdi), %rax
testq %rdi, %rdi
cmoveq %rdi, %rax # derived-to-base conversion: a null pointer stays null
retqAt -O2 the thunk has been optimized into a copy of C::fb with an offset (0x1c - 0x10 = 0xc), with no real jump needed. In the mangled name _ZThn16_, h means a non-virtual adjustment and n16 is −16.
to_b shows that, under multiple inheritance, converting a derived class pointer to a base class pointer ([conv.ptr]) changes the pointer's value, and null pointers must be handled separately.
Virtual bases#
Virtual inheritance gives the common base in a diamond a single subobject ([class.mi]). The cost is that the virtual base's position relative to other base class subobjects is no longer fixed; it depends on the most derived type.
struct V { virtual int fv(); int v = 0; };
struct L : virtual V { virtual int fl(); int l = 1; };
struct R : virtual V { virtual int fr(); int r = 2; };
struct D : L, R { int fv() override; int d = 3; };
int V::fv() { return 0; }
int L::fl() { return 1; }
int R::fr() { return 2; }
int D::fv() { return d; }
V& to_v(L& l) { return l; } 0 | struct L
0 | (L vtable pointer)
8 | int l
16 | struct V (virtual base)
16 | (V vtable pointer)
24 | int v
| [sizeof=32, dsize=28, align=8,
| nvsize=12, nvalign=8]
0 | struct D
0 | struct L (primary base)
0 | (L vtable pointer)
8 | int l
16 | struct R (base)
16 | (R vtable pointer)
24 | int r
28 | int d
32 | struct V (virtual base)
32 | (V vtable pointer)
40 | int v
| [sizeof=48, dsize=44, align=8,
| nvsize=32, nvalign=8]In a standalone L object, V is at offset 16; in D, the L subobject is at offset 0 but V is at offset 32. L's nvsize=12 says that when L is embedded as a base, it brings only its own 12 bytes, and virtual bases are placed at the end by the most derived class.
vbase_offset#
Since the offset isn't fixed, converting L& to V& can only be done by a run-time table lookup. What is looked up is the vbase_offset before offset_to_top in the vtable:
Vtable for 'D' (13 entries).
0 | vbase_offset (32)
1 | offset_to_top (0)
2 | D RTTI
-- (D, 0) vtable address --
-- (L, 0) vtable address --
3 | int L::fl()
4 | int D::fv()
5 | vbase_offset (16)
6 | offset_to_top (-16)
7 | D RTTI
-- (R, 16) vtable address --
8 | int R::fr()
9 | vcall_offset (-32)
10 | offset_to_top (-32)
11 | D RTTI
-- (V, 32) vtable address --
12 | int D::fv()
[this adjustment: 0 non-virtual, -24 vcall offset offset] method: int V::fv()_Z4to_vR1L:
movq %rdi, %rax
movq (%rdi), %rcx # load the vptr
addq -0x18(%rcx), %rax # 3 slots before the vptr: vbase_offset
retqFor the L subobject in D, the vbase_offset is 32; for a standalone L object it is 16. The same to_v code is correct in both cases.
vcall_offset and virtual thunks#
D::fv overrides fv of the virtual base V. When called through a V&, this points to the V subobject and must be adjusted back to the start of D. But the adjustment depends on the most derived type (V is at offset 32 in D, possibly elsewhere in other classes), so it can't be hard-coded in the thunk. So the secondary vtable for V-in-D has an extra vcall_offset (slot 9, value −32), which the thunk reads at run time:
_ZTv0_n24_N1D2fvEv: # virtual thunk to D::fv()
movq (%rdi), %rax # load the V subobject's vptr
movq -0x18(%rax), %rax # the vcall_offset (-32) at vptr - 24
movl 0x1c(%rdi,%rax), %eax # (this + vcall_offset)->d, d is at offset 28 in D
retqv0_n24_ in the mangled name means: first a non-virtual adjustment of 0, then read the vcall_offset at offset −24 from the vptr and add it to this, corresponding exactly to -24 vcall offset offset in the dump.
Construction vtables and the VTT#
The hardest part is construction. By [class.cdtor]/4, while L's constructor is running, virtual calls dispatch to L's versions, and typeid gives L. But at that moment the L subobject lives inside D, where V is at offset 32, not 16 as in a standalone L object. If L's own vtable were used directly, the vbase_offset would be wrong.
For the case of L as a base of D, the Itanium ABI generates a dedicated construction vtable: virtual functions are filled in with L's semantics, offsets with D's layout.
Construction vtable for ('L', 0) in 'D' (8 entries).
0 | vbase_offset (32)
1 | offset_to_top (0)
2 | L RTTI
-- (L, 0) vtable address --
3 | int L::fl()
4 | vcall_offset (0)
5 | offset_to_top (-32)
6 | L RTTI
-- (V, 32) vtable address --
7 | int V::fv()Compared with L's own vtable, only the vbase_offset (16 becomes 32) and the corresponding offset_to_top (−16 becomes −32) differ.
D's constructor passes the addresses of these construction vtables to the base object constructors (C2) of its bases through the VTT (virtual table table, _ZTT1D). This is one of the reasons C1 and C2 must be distinguished. The _ZTT1D generated on our test machine has 7 entries (addends are hexadecimal):
$ llvm-readelf -r vb.o # _ZTT1D is at 0x180 in .data.rel.ro
0000000000000180 _ZTV1D + 18
0000000000000188 _ZTC1D0_1L + 18
0000000000000190 _ZTC1D0_1L + 38
0000000000000198 _ZTC1D16_1R + 18
00000000000001a0 _ZTC1D16_1R + 38
00000000000001a8 _ZTV1D + 60
00000000000001b0 _ZTV1D + 40In order: the address point of D's primary vtable; the two address points of the L-in-D construction vtable (L's own and that of the V in it); the two address points for R-in-D; and the secondary address points of V and R in D's complete vtable. During construction, each time a constructor level is entered, the vptrs of the subobjects are rewritten to the address points for that level. The semantics of the dynamic type changing level by level during construction is implemented exactly this way, by writing the vptr again and again.
Key functions: which TU emits the vtable#
Many TUs may need the same class's vtable, but there should be only one definition of it. The Itanium ABI's rules:
- A class's key function is the first virtual function, at the end of the class definition, that is neither pure nor inline.
- For a class with a key function, the vtable and typeinfo are emitted only in the TU that defines the key function, as strong definitions.
- For a class without a key function, every TU that uses it emits a weak definition of the vtable (in a COMDAT group), deduplicated by the linker. This is called vague linkage; see Object files, symbols and linking.
struct NoKey { virtual int f() { return 1; } };
struct HasKey { virtual int f(); virtual int g() { return 2; } };
struct LateInl { virtual int f(); };
inline int LateInl::f() { return 3; }
int HasKey::f() { return 1; }
int use() { NoKey a; HasKey b; LateInl c; return a.f() + b.g() + c.f(); }$ clang++ -std=c++2c --target=x86_64-linux-gnu -Wweak-vtables -c key.cpp -o key.o
key.cpp:3:8: warning: 'LateInl' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit [-Wweak-vtables]
key.cpp:1:8: warning: 'NoKey' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit [-Wweak-vtables]
$ llvm-nm -C key.o | grep vtable
0000000000000000 V vtable for NoKey
0000000000000000 D vtable for HasKey
0000000000000000 V vtable for LateInlHasKey's key function isf, which this TU defines, so the vtable is a strong definition (D). Other TUs that includeHasKey's definition but don't definefhave only an undefined reference (U).NoKey's virtual functions are all inline, so the vtable is a weak definition (V).-Wweak-vtablespoints this out, because every TU pays the cost of generating it and deduplicating it.LateInl'sfhasn't been declared inline at the end of the class definition, and is only defined as inline afterwards. Clang treats it as also having no key function, and the vtable is a weak definition.
This rule connects to the standard through [basic.def.odr]/8 and [class.virtual]/12: a non-pure virtual function is always odr-used, so it must have a definition. If a key function is declared but its definition forgotten, the vtable isn't emitted in any TU, yet every constructor refers to it. The result is the familiar link error "undefined reference to vtable for X" (GNU ld's wording; Apple ld's output on our test machine is in Object files, symbols and linking). The error points at the vtable rather than the function whose definition was forgotten, which is why it is often baffling.
Questions#
Question 1
struct P { int i; char c; }; is a public type of a library, and user code has struct Q : P { char x; };. A new version of the library only adds an empty user constructor P() {} to P. If users don't recompile and just swap in the new library, what goes wrong?
Answer
Before the constructor is added, P is a POD in the C++03 sense, its dsize is 8, Q::x is at offset 8, and sizeof(Q) is 12. Afterwards P is no longer a POD, its dsize becomes 5, and code compiled against the new header puts Q::x at offset 5, with sizeof(Q) becoming 8.
User code that wasn't recompiled still accesses Q with the old layout. As soon as any code compiled against the new header (such as a template instantiation or inline function in the library) exchanges Q objects with it, the two sides disagree about where x is and how big Q is. At the level of the standard, the same class has different definitions in different TUs, which violates the ODR: the program is ill-formed, no diagnostic required ([basic.def.odr]).
Question 2
Why does calling C::fb through B& use a non-virtual thunk (_ZThn16_), while calling D::fv through V& needs a virtual thunk (_ZTv0_n24_)?
Answer
The offset of the non-virtual base B in C is determined by C's definition alone; whatever larger object a C is embedded in, getting from the B subobject back to the start of C is always minus 16, which can be hard-coded in the thunk. The position of the virtual base V is determined by the most derived type: if D is itself inherited (virtually or not) by another class, the distance between V and the D subobject changes, so the adjustment can only live in the vtable's vcall_offset, read by the thunk at run time.