skip to content

Vtables and Dispatch Cost

Most compilers give each polymorphic object a vptr to a per-class table of function pointers. Interviewers ask what a virtual call costs in a hot loop and when it is devirtualized.

on this pageshow

explore

questions

5

How do mainstream C++ compilers implement a virtual call through a Body&, and what does the C++ standard itself require of that mechanism?

level: middleimportance: must knowfreq 75%

answer

  1. the standard specifies behaviour only
  2. per-class table, per-object pointer
  3. slot index fixed at compile time
  4. load, load, indirect branch
  5. overrides replace slots, new ones append

basics

~20 s

The standard requires only that a virtual call run the final overrider for the object's dynamic type. Compilers use a per-class vtable of function pointers and a hidden per-object vptr: load the vptr, load a fixed slot, branch indirectly.

solid answer

~50 s

The standard defines **behaviour**: a virtual call runs the final overrider for the object's **dynamic type**; it never mentions a vtable. Mainstream ABIs (the **Itanium C++ ABI** used by GCC and Clang outside Windows, and the MSVC ABI) implement it with a read-only **vtable** per polymorphic class and a hidden **vptr** in each object. Each virtual function gets a **slot index** fixed at compile time; a derived class's vtable copies its base's layout, puts its overriders in the inherited slots and appends new virtual functions at the end. A call `b.update(dt)` compiles to three steps: load the vptr from the object, load the function pointer at the slot's offset, and call through it. Clang at `-O2` on arm64 emitted exactly `ldr x8, [x0]`, `ldr x1, [x8, #16]`, `br x1`. In the Itanium layout the table also holds the offset-to-top and a pointer to the class's RTTI before the function slots.

code

cpp · 21 lines
cpp
#include <cstdio>

struct Body {
    virtual ~Body() = default;
    virtual void update(float dt) { x += v * dt; }
    virtual void integrate(float) {}
    float x = 0.0f, v = 1.0f;
};

struct Rigid : Body {
    void update(float dt) override { x += v * dt; v *= 0.99f; }
    virtual void sleep() { v = 0.0f; }
};

void step(Body& b, float dt) { b.update(dt); }   // one indirect call

int main() {
    Rigid r;
    step(r, 0.5f);
    std::printf("%g %g\n", r.x, r.v);
}

go deeper

for a junior

Know the picture: one table of function pointers per class, one hidden pointer per object, and a virtual call that goes through both.

for a middle

Explain slot inheritance, overrides replacing slots and new functions appending, and the load-load-indirect-branch call sequence, while stating the standard never requires any of it.

for a senior

Use the layout to reason about consequences: construction-stage vptr updates, why reordering virtuals breaks binaries, and where the real runtime cost of a call sits.

for a principal

Recognise the vtable layout as an implicit contract in any shared-library interface, and decide where that contract is allowed to change.

## What the standard requires, and what it does not The C++ standard describes virtual functions entirely in terms of **behaviour**. A class with a virtual function is **polymorphic**; a call to a virtual function through a pointer or reference runs the **final overrider** for the object's **dynamic type**; during construction and destruction the dynamic type is the class whose constructor or destructor is running. The words *vtable* and *vptr* do not appear. Everything below is how implementations meet that requirement, and it is one ABI's choice, not a rule of the language. ## The two pieces Both the **Itanium C++ ABI**, used by GCC and by Clang on Linux, macOS and most Unix systems, and the **MSVC ABI** use the same idea: - a **vtable** for each polymorphic class, emitted once into read-only data, holding one function pointer per virtual function; - a **vptr** in each object, normally at offset 0, set by each constructor to point at its class's vtable. Each virtual function gets a **slot index** when the class is compiled. A derived class's vtable starts with the same slots as its base's, in the same order; an **override** replaces the base's pointer in its inherited slot, and a **new** virtual function is appended after the inherited ones. Because the slot of `update` is the same in every class derived from `Body`, code compiled only against `Body`'s declaration can call the right function for a type it has never seen. ## A real Itanium vtable Clang's `-Xclang -fdump-vtable-layouts` prints what it emits. For `Body` with a virtual destructor, `update` and `integrate`, and `Rigid` overriding `update` and adding `sleep`, `Rigid`'s table has seven entries: 1. `offset_to_top`, used to find the complete object from a base subobject; 2. a pointer to `Rigid`'s RTTI, used by `typeid` and `dynamic_cast`; 3. the complete-object destructor and 4. the deleting destructor, two entries for one virtual destructor in this ABI; 5. `Rigid::update`, the override in `Body`'s inherited slot; 6. `Body::integrate`, inherited unchanged; 7. `Rigid::sleep`, appended. The vptr points at entry 3, the first function slot, not at the start of the table, so `update` sits 16 bytes past the address the vptr holds on a 64-bit target. ## The call sequence ```cpp void step(Body& b, float dt) { b.update(dt); } ``` Clang at `-O2` for arm64 compiled this to three instructions: | Instruction | Meaning | |---|---| | `ldr x8, [x0]` | load the vptr from the object | | `ldr x1, [x8, #16]` | load the `update` slot from the vtable | | `br x1` | jump to it (a tail call; a non-tail call uses `blr`) | The work is two dependent loads and an **indirect branch**. None of it is expensive in isolation; the costs that matter in hot code are the cache miss on the first load when objects are scattered, the indirect branch when its target keeps changing, and the lost inlining, which belong to the question of dispatch cost. ## Consequences that follow from the layout - **Construction stages.** Each constructor sets the vptr to its own class's table before its body runs, which is how a virtual call inside a base constructor reaches the base version. - **Virtual functions cannot be inlined through a base reference** unless the compiler can prove the dynamic type; that is devirtualization. - **Inserting or reordering virtual functions, or adding one to a base class, changes slot indices**, so code compiled against the old class calls the wrong slot; this is why it breaks binary compatibility. - **Pure virtual slots are not empty**: in the Itanium ABI they hold a runtime error handler that aborts if the slot is ever called. None of this is portable knowledge in the standard's sense, but it is stable across the compilers you will meet, and it is what an interviewer means by "explain the vtable". ## Seeing it in your own build You do not have to take the layout on trust; each toolchain will show it: - **Clang:** `-Xclang -fdump-vtable-layouts` prints every vtable it emits, slot by slot, as in the dump above. - **GCC:** its C++ class-hierarchy dump, one of the `-fdump-lang` options, writes the same information, virtual tables included, to a dump file. - **Any object file:** `nm -C` lists symbols such as `vtable for Rigid` and `typeinfo for Rigid`, showing which file defines each table. - **Generated code:** `-O2 -S` shows the load, load, indirect-branch sequence at each call site, and whether a given call survived as indirect.

  • Why does a virtual destructor take two vtable entries in the Itanium ABI?
    The ABI emits a complete-object destructor, which destroys the object, and a deleting destructor, which destroys it and then calls the right `operator delete`. `delete basePtr` calls the deleting entry, so the deallocation also uses the dynamic type. Clang's dump shows both, marked `[complete]` and `[deleting]`. This split is that ABI's design, not something the standard specifies.
  • Why can the compiler emit a call through a fixed offset for a class it has never seen?
    Because slot indices are inherited: every class derived from `Body` keeps `Body`'s slots in the same order and only replaces pointers or appends new ones. Code compiled against `Body` knows `update` is, say, the third function slot, and that holds for any derived vtable at run time.

Every handset of one phone model points to the same printed speed-dial card; pressing 3 dials whatever that model's card lists in slot 3. The caller only needs the slot number, never the model.

  • The C++ standard defines the vtable layout every compiler must use.
  • The vptr points to the first byte of the vtable, the offset-to-top entry.
  • Each virtual call looks the function up by name in the vtable.
  • A derived class's vtable stores only its own new virtual functions.
  • Overriding a function appends a new slot after the base's slots.
open as a page

In C++, why does adding a virtual function to a struct with one float grow sizeof from 4 to 16 on 64-bit targets?

level: juniorimportance: should knowfreq 62%

basics

~20 s

Mainstream ABIs add a hidden vtable pointer to every polymorphic object: 8 bytes on a 64-bit target. The struct's alignment becomes 8, so 8 + 4 bytes round up to 16. More virtual functions add no further per-object size.

open as a page

Why does linking a C++ program fail with an undefined vtable for Body when Body's constructor and other members compile fine?

level: middleimportance: should knowfreq 45%

basics

~20 s

Under the Itanium C++ ABI, Body's vtable is emitted only in the translation unit that defines its key function, the first non-inline, non-pure virtual function. If that function is never defined or its file is not linked, no vtable exists.

open as a page

When can a C++ compiler turn a virtual update() call into a direct call, and why does marking Particle final not help calls through Body&?

level: seniorimportance: should knowfreq 45%

basics

~20 s

When it can prove the override (final static type, visible dynamic type, one implementation program-wide) or guards a guess with a runtime check. Through Body&, final on Particle proves nothing: any class derived from Body could be there.

open as a page

A profiler flags a C++ physics loop calling virtual update() on 100,000 bodies each frame; what does each call really cost, and how do you cut it?

level: seniorimportance: should knowfreq 55%

basics

~20 s

Each call loads the object's vptr, often a cache miss when bodies are scattered on the heap, then makes an indirect branch that mispredicts when types interleave, and blocks inlining. Group bodies by type and dispatch once per batch.

open as a page