0
#include <iostream>
using namespace std;
class A
{
    public:
    int val;
    char c1;
};
class B: public A
{
    public:
    char c2;
};
class C: public B
{
    public:
    char c3;
};

int main()
{
    cout << sizeof(A) << ", " << sizeof(B) << ", " << sizeof(C) << endl;
    return 0;
}

The output is 8, 12, 12. The size of B is 12 because there are 3 bytes padding after A::c1. But why the size of C is 12 too? Aren't there bytes padding after B::c2? (The compiler I used is gcc 4.8)

C.Jx
  • 11
  • 2
  • 2
    https://stackoverflow.com/questions/25479373/how-memory-is-allocated-for-private-and-public-members-of-the-class – Cory Kramer Aug 16 '17 at 13:01
  • Possible byte padding (answer to second question)? – CinCout Aug 16 '17 at 13:01
  • Possible duplicate of [How memory is allocated for private and public members of the class](https://stackoverflow.com/questions/25479373/how-memory-is-allocated-for-private-and-public-members-of-the-class) – Raindrop7 Aug 16 '17 at 13:07
  • with c++11 you [could check the alignment yourself](http://en.cppreference.com/w/c/language/_Alignof) – Austin_Anderson Aug 16 '17 at 13:35

0 Answers0