Custom class size

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

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

class cl2 {                                                                                                                                                                               
public:                                                                                                                                                                                   
    char cb;                                                                                                                                                                              
    cl2();                                                                                                                                                                                
    ~cl2();                                                                                                                                                                               
};                                                                                                                                                                                        
+3
source share
5 answers

The compiler can freely insert additions between and after class members to ensure proper alignment of variables, etc. Exactly what is added depends on the implementation. In this case, I assume that the compiler adds 3 bytes of padding after cl1::cb, perhaps to ensure that the next variable in memory is aligned on a 4-byte boundary.

+6
source

cl1 (n) - 4 , cl1 4 ( 8), cl1 n, 4- . , ( ), ( ).

, .

+3

- . , ,

, , . , . , , - , .

+2

- . , #pragma pack (1), 5 1, .

+2

As you study the size of the structure and how it fills, let me tell you an interesting thing. The size of the structure depends not only on the members , but also on the order of their announcement . For example, the size of the following structures is different, although both have the same number of members of the same type, the only difference is the order in which they are declared!

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;

Conclusion:

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

Ideone online demo: http://www.ideone.com/8OoxX

+2
source

Source: https://habr.com/ru/post/1793894/


All Articles