zhanwu
zhanwu

Reputation: 1528

size of a user defined class

why the size of the class cl1 in the following code is 8 but not 5, while the size of class cl2 is 1?

class cl1 {                                                                                                                                                                               
public:                                                                                                                                                                                   
    int n;                                                                                                                                                                                
    char cb;                                                                                                                                                                              
    cl1();                                                                                                                                                                                
    ~cl1();                                                                                                                                                                               
};                                                                                                                                                                                        

class cl2 {                                                                                                                                                                               
public:                                                                                                                                                                                   
    char cb;                                                                                                                                                                              
    cl2();                                                                                                                                                                                
    ~cl2();                                                                                                                                                                               
};                                                                                                                                                                                        

Upvotes: 3

Views: 1007

Answers (5)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361812

While you're exploring the size of struct and how padding is done, let me tell you an interesting thing. The size of struct not only depends on the members, but also on the order of their declaration. For example, size of the following structs is different, even though both has same number of members of same types, the only difference is the order of their declaration!

struct A
{
  int a;
  char b;
  char c;
};

struct B
{
  char b;
  int a;
  char c;
};

cout << "sizeof(A) = " << sizeof(A) << endl;
cout << "sizeof(B) = " << sizeof(B) << endl;

Output:

sizeof(A) = 8
sizeof(B) = 12

Online Demo at Ideone: http://www.ideone.com/8OoxX

Upvotes: 2

Mike DeSimone
Mike DeSimone

Reputation: 42845

The largest member of cl1 (n) is 4 bytes, so the size of cl1 is padded up to the nearest 4 bytes (8 in this case) so that an array of cl1 objects does not create n members which are not aligned to 4-byte addresses. Most processors really hate misaligned multi-byte values, either suffering performance losses (two memory cycles to access one value) or outright crashes (alignment exceptions).

There is no guarantee that this will be consistent from compiler to compiler or platform to platform.

Upvotes: 3

Cthutu
Cthutu

Reputation: 8917

It is because of structure padding by the compiler. If you want to remove the padding, try #pragma pack(1) and you should get 5 and 1 as expected.

Upvotes: 2

Armen Tsirunyan
Armen Tsirunyan

Reputation: 133122

This is all due to padding. More info can be found, for example, here

The thing is that the addresses of both the object and its members should be properly aligned for OS and Hardware - specific reasons. Thus the result. The problem of padding is complicated by the fact that objects in an array must be located consecutively, without any space in between, and ALL should be properly aligned.

Upvotes: 2

Nick Meyer
Nick Meyer

Reputation: 40422

The compiler is free to insert padding in between and after class members in order to ensure that variables are properly aligned, etc. Exactly what padding is inserted is up to the implementation. In this case, I'd guess that the compiler is adding 3 bytes of padding after cl1::cb, perhaps to ensure that the next variable in memory is aligned on a 4-byte boundary.

Upvotes: 6

Related Questions