Stack Exchange
Size of singly inherited derived classes
The following bit of code (online version) is based on Section 3.4 of the book "Inside the C++ Object Model" by Stanley Lippman (see pgs 85-86 of the hardcopy or page 64 of the PDF version). 
#include <iostream>
#include <cstddef> // defines offset

struct Base
{
    int  w;
    char x;
};

struct Derived1 : Base
{
    char y;
};

struct Derived2 : Derived1
{
    char z;
};

int main()
{
    std::cout << "sizeof(Base) = " << sizeof( Base ) << "\n";
    std::cout << "sizeof(Derived1) = " << sizeof( Derived1 ) << "\n";
    std::cout << "sizeof(Derived2) = " << sizeof( Derived2 ) << "\n";

    std::cout << "Offset of x in Derived2 = " << offsetof( Derived2 , x) << "\n";
    std::cout << "Offset of y in Derived2 = " << offsetof( Derived2 , y) << "\n";
    std::cout << "Offset of z in Derived2 = " << offsetof( Derived2 , z) << "\n";
}

According to the text Derived2 is supposed(?) to be 16 bytes in size (assuming 32 bit machine) but on gcc I'm getting 12 bytes. On Visual studio however its size is correctly(?) reported as 16 bytes. Is this a gcc regression bug or is this something that the standard does not define?
Edit
Stanley Lippman did NOT use the offsetof macro. That's my doing.

I would just say gcc did a better job with padding than visual studio. There is no standard saying on how the object should be represented. It's upto the implementation.

You're reading an edition from 1996, which is two decades ago and two years before even the first standard version of C++ was released. Try to find an up-to-date version; don't be too shy to actually purchase proper educational material.

I believe GCC('s ABI) is needlessly leaving optimization room on the table. All three classes could easily have the same size 8 on both 64 and 32 bit x86, by laying it out as 4+1+1+1+padding. I think the Itanium ABI doesn't allow that, though.

Check with link to understand if the rule of padding applies to your code.

The macro offsetof shall only be used on standard-layout types. Since neither Derived1 nor Derived2 have standard layout, the behaviour of your is undefined.
Besides that, the C++ standard doesn't make any particular promise about the layout of non-static data members, so it isn't really a "bug" if one layout doesn't match your expectations for another layout.

Disappointing from Lippman! UB in an example, and assuming x amount of padding! In his defence, this book was written in 1996...

The provided code seems to be pretty standard. I guess the issue with using offsetof arises only with multiple and virtual inheritance.

@Arunmu: No, Kerrek is right; these classes do not meet the definition of standard-layout. Read §9/7.

@LightnessRacesinOrbit oh..Thanks!
