In C++, why does adding a virtual function to a struct with one float grow sizeof from 4 to 16 on 64-bit targets?
answer
- a hidden member the compiler adds
- one per object, not per function
- alignment of a pointer
- the table itself is shared per class
basics
~20 sMainstream 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.
solid answer
~50 sThe standard says nothing about object layout here, but every mainstream ABI implements virtual calls with a **vptr**, a hidden pointer stored in each object of a polymorphic class that points to the class's shared **vtable**. On a 64-bit target the pointer is 8 bytes and 8-byte aligned, so `struct { float m; virtual void f(); }` holds 8 + 4 bytes and is padded to 16, a multiple of its new alignment. The cost is **one vptr per object** under single inheritance, however many virtual functions the class has; the table of function pointers exists once per class. Adding a virtual function also makes the type **non-trivially copyable** and not standard-layout, so it can no longer be copied with `std::memcpy` or written raw to a file. Across 100,000 physics bodies that is 800 KB of vptrs before padding.
code
cpp · 17 lines#include <cstdio>
struct Plain { float mass; };
struct OneVirt { float mass; virtual void update(float) {} };
struct ManyVirt { float mass; virtual void update(float) {} virtual void integrate(float) {}
virtual void collide() {} virtual ~ManyVirt() = default; };
struct Empty { };
struct EmptyVirt { virtual ~EmptyVirt() = default; };
int main() {
std::printf("Plain %zu\n", sizeof(Plain));
std::printf("OneVirt %zu\n", sizeof(OneVirt));
std::printf("ManyVirt %zu\n", sizeof(ManyVirt));
std::printf("Empty %zu\n", sizeof(Empty));
std::printf("EmptyVirt %zu\n", sizeof(EmptyVirt));
std::printf("alignof(OneVirt) %zu\n", alignof(OneVirt));
}go deeper
Recall that a class with virtual functions gets a hidden pointer per object, usually 8 bytes on 64-bit, plus padding to its new alignment.
Explain the split: one vtable per class holds the function pointers, one vptr per object points to it, and alignment rounds the size up.
Bring in the consequences: lost trivial copyability, cache footprint across large arrays, and when small numerous objects should not be polymorphic at all.
Turn it into a design guideline: keep hot, numerous value types non-polymorphic and put dispatch at the batch or system level, backed by measurement.
## Where the extra bytes come from The C++ standard specifies what a virtual call does, not how. In practice every mainstream compiler, whether it follows the **Itanium C++ ABI** (GCC, and Clang on Linux, macOS and most Unix systems) or the **MSVC ABI**, implements dynamic dispatch with two pieces: - a **vtable** per polymorphic class: a read-only array of function pointers, one slot per virtual function, shared by every object of that class; - a **vptr** per object: a hidden pointer, normally at offset 0, that points into its class's vtable. So the per-object price is one pointer, and the per-class price is one table. A run on a 64-bit target with Apple clang 21 shows it directly: | Type | `sizeof` | |---|---| | `struct Plain { float mass; }` | 4 | | same plus one virtual function | 16 | | same plus four virtual functions | 16 | | empty struct | 1 | | empty struct with only a virtual destructor | 8 | ## Why 16 and not 12 The vptr is 8 bytes and needs **8-byte alignment** on a 64-bit target. A class's alignment is the largest alignment of its members, hidden ones included, so the struct's alignment rises from 4 to 8. Its size must be a multiple of its alignment, so that elements of an array stay aligned, and 8 + 4 = 12 is rounded up to **16**. A quarter of each object is now padding. Member order cannot remove it here, but in larger types grouping small members together keeps the padding to one tail. ## What does not grow 1. **More virtual functions** add slots to the class's vtable, not bytes to each object: four virtual functions cost the same 16 bytes as one. 2. **Deeper single inheritance** reuses the same vptr. A `Rigid` derived from `Body` gets its own vtable, but each `Rigid` object still has one vptr at offset 0, pointing at `Rigid`'s table. 3. **Static members and non-virtual functions** never occupy space in objects. Multiple inheritance is different: an object can carry one vptr per polymorphic base subobject, which belongs with the material on multiple and virtual bases. ## Costs beyond the bytes - **No more raw copies.** A class with a virtual function is neither **trivially copyable** nor **standard-layout**. Copying it with `std::memcpy` is not allowed, and writing it to a file or shared memory stores a vptr that is a code address in one process, meaningless in another. - **Cache footprint.** In a physics engine that updates 100,000 bodies per frame, the vptrs alone are 800 KB on a 64-bit target, plus padding. If the hot loop touches only position and velocity, every cache line also carries pointers the loop never needs. - **No zero-overhead empty bases.** An empty class can take no space as a base subobject, but a polymorphic empty class has a vptr, so it costs 8 bytes wherever it appears. ## When it is worth avoiding The vptr is cheap for objects that are few or large, such as systems, devices and services. It matters for objects that are **small and numerous**: particles, mesh vertices, network packets, entries in a hot array. For those, keep the type free of virtual functions and put the polymorphism one level up, on the container or system that processes a whole batch, or use a closed set of types in a `std::variant`. Whether that redesign pays off is a measurement question: compare the real loop, not `sizeof` alone. ## Pinning the assumption in code When a layout matters, state it where the compiler checks it instead of in a comment: ```cpp #include <type_traits> struct Body { virtual ~Body() = default; virtual void update(float); float x, v; }; static_assert(std::is_polymorphic_v<Body>); static_assert(std::has_virtual_destructor_v<Body>); static_assert(sizeof(Body) == 16, "vptr + two floats on this ABI"); ``` - `std::is_polymorphic_v` is true exactly when the class declares or inherits a virtual function, so it flags a type that silently became polymorphic. - `std::has_virtual_destructor_v` confirms a base is safe to delete through. - The `sizeof` assertion is ABI-specific by design: it documents the 64-bit layout the code was tuned for and fails loudly on a target where the numbers differ.
- Does a class derived from a polymorphic base get a second vptr?Not under single inheritance in common ABIs: the derived object reuses the base's vptr slot at offset 0, and the constructor points it at the derived class's own vtable. A class with several polymorphic bases is different, since each such base subobject can carry its own vptr, so an object may hold more than one.
- Why can you not std::memcpy a polymorphic object into a buffer and back?A class with a virtual function is not trivially copyable, so the standard does not allow copying its bytes into another object. Practically, the vptr is an address inside one running program; a byte copy written to disk or sent to another process carries a pointer that is meaningless there. Copy through constructors, or serialise the data members explicitly.
- Each virtual function adds another pointer to every object.
- The standard requires a vptr at offset 0 of every polymorphic object.
- Each object stores its own private copy of the vtable.
- Virtual functions only cost time per call, never memory per object.
- A polymorphic struct can still be copied safely with std::memcpy.