Skip to main content
Be Bitwise
C++What Your C++ Code Actually Does

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

cpp
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):

cpp
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:

cpp
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:

cpp
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:

asm
; 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:

  1. Set up this in rdi
  2. Load the vtable pointer from the object (the first 8 bytes of any polymorphic object)
  3. 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, rax
mov rax, QWORD PTR [rdi]
call QWORD PTR [rax]
*shapes[0], a Circle at 0x4052a0
0x403d60
+0 vptr
2.0
+8 radius
vtable for Circle, from 0x403d60
Circle::area
[0] +0
~Circle D1
[1] +8
~Circle D0
[2] +16
rax = 0x4052a0rdi = -call target = -
i = 0. The loop has loaded shapes[0] into rax: the address of a Circle. The code has no idea it's a Circle.
Step 1/8

Virtual vs Non-Virtual Call Comparison

cpp
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();
}
asm
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
ret

A 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:

  1. 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.

  2. 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.

  3. 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:

cpp
void example() {
Circle c(5.0);
double a = c.area(); // Compiler knows c is a Circle
}
asm
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:

asm
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

cpp
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:

asm
; 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
ret

Inside 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.

Step 1 of 10

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)

  1. Derived::Derived(): ; rdi = this; rbx saved in the prologue (not shown)
  2. mov rbx, rdi ; keep this in a callee-saved register
  3. call Base::Base() ; build the Base part first
  4. mov QWORD PTR [rbx], OFFSET _ZTV7Derived+16 ; vptr = Derived's vtable
  5. mov DWORD PTR [rbx+12], 2 ; y = 2
  6. ret
  7. Base::Base(): ; rdi = this; rbx saved in the prologue (not shown)
  8. mov rbx, rdi
  9. mov QWORD PTR [rbx], OFFSET _ZTV4Base+16 ; vptr = Base's vtable
  10. mov DWORD PTR [rbx+8], 1 ; x = 1
  11. mov rax, QWORD PTR [rbx] ; this->foo(): load the vptr
  12. mov rdi, rbx
  13. call QWORD PTR [rax] ; slot 0, indirect call
  14. ret

Registers

rbx
-
rax
-
target
-
rdi
0x4052a0

Derived object at 0x4052a0 (16 bytes)

+0vptr? (not written yet)
+8x (int)?
+12y (int)?

Functions in .text

  • 0x401140 Base::foo
  • 0x401170 Derived::foo

vtable for Base (_ZTV4Base at 0x403d20)

-16offset-to-top0
-8typeinfo0x403f00 (typeinfo for Base)
+0[0] foo0x401140 Base::foo

vtable for Derived (_ZTV7Derived at 0x403d38)

-16offset-to-top0
-8typeinfo0x403f20 (typeinfo for Derived)
+0[0] foo0x401170 Derived::foo

Multiple Inheritance and Virtual Bases

Throw multiple inheritance in and each base may bring its own vtable pointer along:

cpp
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:

asm
; B *bptr = c_ptr; // Pointer adjustment
lea rax, [rdi+16] ; Adjust pointer to B subobject

Practical Implications

Once vtables click, several C++ rituals stop feeling arbitrary:

  • Abstract classes refuse to instantiate. The compiler rejects new Shape outright, 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_ptr calls Base::~Base() directly and skips the derived destructor entirely. With one, the call routes through the vtable to the correct destructor.

  • final is a performance hint. Marking a class or function final tells 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.