Padding and Alignment

Alignment with Virtual Functions

How does alignment work with inheritance and virtual functions?

Abstract art representing computer programming

Inheritance and virtual functions add some complexity to memory alignment. Let's break this down by looking at different scenarios:

Basic Virtual Function Impact

Here’s a code example that uses virtual functions:

#include <iostream>

// No virtual functions
struct Basic {
  int Value;
  char Tag;
};

// With virtual functions
struct Virtual {
  virtual void Method() {}
  int Value;
  char Tag;
};

int main() {
  std::cout << "Size without virtual: "
    << sizeof(Basic) << " bytes\n"
    << "Size with virtual: "
    << sizeof(Virtual) << " bytes\n"
    << "Alignment without virtual: "
    << alignof(Basic) << " bytes\n"
    << "Alignment with virtual: "
    << alignof(Virtual) << " bytes\n";
}
Size without virtual: 8 bytes
Size with virtual: 16 bytes
Alignment without virtual: 4 bytes
Alignment with virtual: 8 bytes

The size increase comes from the virtual function table pointer (vptr) added to objects with virtual functions. This pointer needs proper alignment too!

Inheritance Alignment

Here’s an example that shows some inheritance scenarios:

#include <iostream>

class Base {
  int BaseValue;
  char BaseTag;
};

class Derived : public Base {
  double DerivedValue;
  char DerivedTag;
};

class VirtualBase {
  int BaseValue;
  char BaseTag;
  virtual void Method() {}
};

class VirtualDerived : public VirtualBase {
  double DerivedValue;
  char DerivedTag;
  virtual void Method() override {}
};

int main() {
  std::cout << "Regular inheritance:\n"
    << "  Base size: " << sizeof(Base)
    << " bytes\n"
    << "  Derived size: " << sizeof(Derived)
    << " bytes\n"

    << "\nVirtual inheritance:\n"
    << "  Base size: " << sizeof(VirtualBase)
    << " bytes\n"
    << "  Derived size: " << sizeof(VirtualDerived)
    << " bytes\n";
}
Regular inheritance:
  Base size: 8 bytes
  Derived size: 24 bytes

Virtual inheritance:
  Base size: 16 bytes
  Derived size: 32 bytes

Key points about inheritance and alignment:

  • The vptr is typically stored at the start of the object
  • Base class alignment affects derived class alignment
  • Virtual inheritance may add additional overhead
  • Multiple inheritance can lead to complex alignment requirements
This Question is from the Lesson:

Padding and Alignment

Learn how memory alignment affects data serialization and how to handle it safely

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Padding and Alignment

Learn how memory alignment affects data serialization and how to handle it safely

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 75 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2025 - All Rights Reserved