Virtual Functions & Dynamic Dispatch
Virtual functions are how C++ does polymorphism. Call one through a base-class pointer and you get the correct derived-class implementation, picked at runtime. The mechanism is dynamic dispatch, and it rides on a per-class structure called the vtable. We'll pull a vtable apart, watch a virtual call lower to assembly, and weigh up what that indirection actually costs.
A Simple Polymorphism Example
class Shape {public: virtual double area() const = 0; virtual ~Shape() {}};
class Circle : public Shape { double radius;public: Circle(double r) : radius(r) {} double area() const override { return 3.14159265 * radius * radius; }};
class Rectangle : public Shape { double width, height;public: Rectangle(double w, double h) : width(w), height(h) {} double area() const override { return width * height; }};
double total_area(Shape **shapes, int n) { double sum = 0; for (int i = 0; i < n; i++) { sum += shapes[i]->area(); // Virtual call } return sum;}The Vtable Structure
Whenever a class has virtual functions, the compiler emits a vtable for it: a static array, one per class, that's mostly function pointers. Under the Itanium C++ ABI used by GCC and Clang on Linux, two header words sit in front of the function slots (the offset-to-top and a pointer to the class's type_info), and a virtual destructor takes two slots: a complete-object destructor (D1) and a deleting destructor (D0) that also frees the memory.
Shape's vtable (abstract - never instantiated directly):
vtable for Shape: [-2] offset-to-top = 0 [-1] -> typeinfo for Shape [0] -> __cxa_pure_virtual (area() is pure; calling it aborts) [1] -> Shape::~Shape() (complete-object destructor, D1) [2] -> Shape::~Shape() (deleting destructor, D0)Circle's vtable:
vtable for Circle: [-2] offset-to-top = 0 [-1] -> typeinfo for Circle [0] -> Circle::area() [1] -> Circle::~Circle() (D1) [2] -> Circle::~Circle() (D0)Rectangle's vtable:
vtable for Rectangle: [-2] offset-to-top = 0 [-1] -> typeinfo for Rectangle [0] -> Rectangle::area() [1] -> Rectangle::~Rectangle() (D1) [2] -> Rectangle::~Rectangle() (D0)The object's vtable pointer points at slot 0, past the two header words, which is why you'll see constructors store _ZTV...+16 further down. Each virtual function lives at a fixed slot. area() always sits at index 0 and the destructors at indices 1 and 2, so delete shape calls through [vptr+16]. Overriding works precisely because the derived class drops its own function pointer into the same slot.
How a Virtual Call Compiles
The virtual call shapes[i]->area() compiles to:
; Assume rax = shapes[i] (pointer to a Shape object) mov rdi, rax ; 'this' = the object pointer mov rax, QWORD PTR [rdi] ; Load vtable pointer (first 8 bytes of object) call QWORD PTR [rax] ; Call the function at vtable[0] (area)Three instructions:
- Set up
thisinrdi - Load the vtable pointer from the object (the first 8 bytes of any polymorphic object)
- Call through the vtable at the slot for
area(offset 0)
That third step is an indirect call, with the target address fetched from memory at runtime. The CPU has no idea which function it's about to enter until that vtable read completes.
Step through total_area making the call twice, once for a Circle and once for a Rectangle. The addresses and values are made up, but the layout follows the Itanium ABI described above. Watch which line of the code changes between the two calls. None of it does.
shapes[i]->area() for a Circle, then a Rectangle
; rax = shapes[i]mov rdi, raxmov rax, QWORD PTR [rdi]call QWORD PTR [rax]
Virtual vs Non-Virtual Call Comparison
class Widget {public: int value; virtual int get_virtual() { return value; } int get_direct() { return value; }};
void example(Widget *w) { int a = w->get_virtual(); int b = w->get_direct();}example: push rbx mov rbx, rdi ; Save 'w'
; Virtual call: w->get_virtual() mov rax, QWORD PTR [rdi] ; Load vtable pointer call QWORD PTR [rax] ; Indirect call through vtable ; eax = result
; Direct call: w->get_direct() mov rdi, rbx ; 'this' = w call _ZN6Widget10get_directEv ; Direct call - address known at compile time ; eax = result
pop rbx retA non-virtual call is a call to a baked-in address. The CPU knows where it's going and can prefetch instructions ahead of time. A virtual call costs two memory loads, the vtable pointer and then the function pointer, plus an indirect jump. The pipeline has to twiddle its thumbs until those loads land before it can branch.
The Cost of Indirection
What virtual dispatch costs you over a direct call:
-
Two extra memory loads. The vtable pointer, then the function pointer. Hot code keeps these in L1, so each is roughly 4 cycles. A cache miss is a different story.
-
Branch prediction penalty. Direct calls always head to the same address. Indirect calls can land on any number of functions depending on the runtime type, and the predictor has to keep up. Modern indirect branch predictors are clever, but they're not as accurate as the direct-call kind.
-
No inlining. The compiler can't inline a virtual call, since it doesn't know which body it's calling. A small direct call, by contrast, often gets inlined and the overhead vanishes outright.
In a tight loop calling a virtual function millions of times, you can be looking at 2-5x the cost of an inlined direct call. Outside of hot paths, it's noise.
Devirtualisation
Sometimes the compiler can pin down the concrete type at compile time and swap the virtual call out for a direct one:
void example() { Circle c(5.0); double a = c.area(); // Compiler knows c is a Circle}example: ; The compiler knows c is Circle, so it devirtualizes: lea rdi, [rbp-16] ; 'this' = &c call _ZNK6Circle4areaEv ; Direct call to Circle::area() ; No vtable lookup!With -O2, the compiler may even inline the call entirely:
example: movsd xmm0, QWORD PTR [rbp-8] ; Load radius mulsd xmm0, xmm0 ; radius * radius mulsd xmm0, QWORD PTR .LC_PI ; * pi ; No function call at all!Devirtualisation kicks in whenever the compiler can prove the runtime type. Local variables make this easy. So do dynamic_cast checks, and link-time optimisation that can see only one class ever overrides a given virtual.
Object Layout with Virtual Functions
class Base {public: int x; virtual void foo() {}};
class Derived : public Base {public: int y; void foo() override {}};Layout of a Derived object:
y lands at offset 12, inside what would be Base's tail padding. The Itanium ABI lets a derived class reuse the tail padding of a non-POD base, and a class with virtual functions is never POD, so sizeof(Derived) is 16, the same as sizeof(Base).
The vtable pointer always sits at offset 0, so casting between Base* and Derived* under single inheritance needs zero pointer arithmetic. The vtable lookup at offset 0 works regardless of which static type you reach the object through.
Constructor Vtable Setup
As construction unfolds, the vtable pointer keeps getting rewritten to whichever class is currently being built. That's why calling a virtual function inside a constructor dispatches to the base version:
; Base::Base() constructor_ZN4BaseC1Ev: push rbp mov rbp, rsp mov QWORD PTR [rbp-8], rdi ; Store 'this' mov rax, QWORD PTR [rbp-8] mov QWORD PTR [rax], OFFSET _ZTV4Base+16 ; Set vtable to Base's vtable ; Initialize x... pop rbp ret
; Derived::Derived() constructor_ZN7DerivedC1Ev: push rbp mov rbp, rsp mov QWORD PTR [rbp-8], rdi ; First, call Base constructor call _ZN4BaseC1Ev ; This sets vtable to Base's vtable ; Now override vtable pointer mov rax, QWORD PTR [rbp-8] mov QWORD PTR [rax], OFFSET _ZTV7Derived+16 ; Switch to Derived's vtable ; Initialize y... pop rbp retInside Base::Base(), the vtable still points at Base. Only once Base::Base() returns does Derived's constructor overwrite the slot with Derived's vtable. Virtual calls made during construction resolve to the base class because the derived vtable simply hasn't been wired up yet.
Step through a new Derived below, where Base's constructor calls foo(). Keep an eye on the vptr: it's written twice, and which write has happened decides the foo you get.
Constructing a Derived: the vptr is written twice
Base's constructor calls the virtual foo(). Step through and watch which vtable the vptr holds when that call happens. Addresses are illustrative; the layout follows the Itanium C++ ABI on x86-64.
new Derived has allocated 16 bytes at 0x4052a0 and called Derived's constructor. Nothing is written yet, so the vptr and fields hold leftover bytes.
x86-64 (Intel syntax)
- Derived::Derived(): ; rdi = this; rbx saved in the prologue (not shown)
- mov rbx, rdi ; keep this in a callee-saved register
- call Base::Base() ; build the Base part first
- mov QWORD PTR [rbx], OFFSET _ZTV7Derived+16 ; vptr = Derived's vtable
- mov DWORD PTR [rbx+12], 2 ; y = 2
- ret
- Base::Base(): ; rdi = this; rbx saved in the prologue (not shown)
- mov rbx, rdi
- mov QWORD PTR [rbx], OFFSET _ZTV4Base+16 ; vptr = Base's vtable
- mov DWORD PTR [rbx+8], 1 ; x = 1
- mov rax, QWORD PTR [rbx] ; this->foo(): load the vptr
- mov rdi, rbx
- call QWORD PTR [rax] ; slot 0, indirect call
- ret
Registers
- rbx
- -
- rax
- -
- target
- -
- rdi
- 0x4052a0
Derived object at 0x4052a0 (16 bytes)
| +0 | vptr | ? (not written yet) |
| +8 | x (int) | ? |
| +12 | y (int) | ? |
Functions in .text
- 0x401140 Base::foo
- 0x401170 Derived::foo
vtable for Base (_ZTV4Base at 0x403d20)
| -16 | offset-to-top | 0 |
| -8 | typeinfo | 0x403f00 (typeinfo for Base) |
| +0 | [0] foo | 0x401140 Base::foo |
vtable for Derived (_ZTV7Derived at 0x403d38)
| -16 | offset-to-top | 0 |
| -8 | typeinfo | 0x403f20 (typeinfo for Derived) |
| +0 | [0] foo | 0x401170 Derived::foo |
Multiple Inheritance and Virtual Bases
Throw multiple inheritance in and each base may bring its own vtable pointer along:
class A { public: virtual void fa() {} int a; };class B { public: virtual void fb() {} int b; };class C : public A, public B { public: void fa() override {} void fb() override {} int c; };Layout of C:
The gap at offset 12 stays because B's vtable pointer needs 8-byte alignment. c reuses B's tail padding, so the whole object is 32 bytes.
A C object carries TWO vtable pointers. Casting a C* to a B* makes the compiler add 16 bytes to land on the B subobject:
; B *bptr = c_ptr; // Pointer adjustment lea rax, [rdi+16] ; Adjust pointer to B subobjectPractical Implications
Once vtables click, several C++ rituals stop feeling arbitrary:
-
Abstract classes refuse to instantiate. The compiler rejects
new Shapeoutright, because the language says a class with a pure virtual can't be instantiated. The vtable still has an entry for each pure virtual: on GCC and Clang it points at__cxa_pure_virtual, which aborts if you manage to reach it (say, from a helper the base constructor calls). -
Virtual destructors are non-negotiable. Without one,
delete base_ptrcallsBase::~Base()directly and skips the derived destructor entirely. With one, the call routes through the vtable to the correct destructor. -
finalis a performance hint. Marking a class or functionfinaltells the compiler no further overriding will happen, which clears the way for devirtualisation. -
CRTP exists for a reason. The Curiously Recurring Template Pattern gives you static polymorphism, dispatch resolved at compile time, with zero vtable overhead.
In absolute terms, virtual dispatch is cheap. In a hot loop it isn't. When the profiler fingers virtual calls as the bottleneck, the usual moves are to mark functions final so the compiler can devirtualise, to switch to CRTP for static polymorphism, or to reorganise the data so objects of the same type are batched together and called directly.