skip to content
Ivan's wiki
Site Tools
Search
Tools
Show pagesource
Backlinks
Recent Changes
Media Manager
Sitemap
>
Recent Changes
Media Manager
Sitemap
cpp-object-layout
This page is read only. You can view the source, but not change it.
# C++ object layout **[Object layout](https://en.cppreference.com/w/cpp/language/object)** describes how a class's members are arranged in memory. The standard guarantees that non-static member variables are laid out in declaration order and that empty base classes may be optimized away. Virtual function pointers (vtable pointers) are typically stored at the start or end of the object, and alignment padding is added as needed. Understand object layout when writing code that depends on memory layout (serialization, hardware buffers), but avoid making assumptions beyond the standard's guarantees. ## Example This example shows memory layout of objects with inheritance and virtual functions. ```cpp // compile: g++ -o layout layout.cpp // run: ./layout // description: inspect object layout with sizeof and alignment #include <iostream> class Base { public: int x; virtual void func() {} }; class Derived : public Base { public: int y; char z; }; struct NoVirtual { int a; char b; double c; }; int main() { std::cout << "Base size: " << sizeof(Base) << "\n"; std::cout << "Derived size: " << sizeof(Derived) << "\n"; std::cout << "NoVirtual size: " << sizeof(NoVirtual) << "\n"; // Note: vtable pointer adds overhead to Base // Padding aligns members for efficient access return 0; } ```
Page Tools
Show pagesource
Backlinks
Back to top