@@ -7,45 +7,151 @@ struct Z1
77 int i :3 ;
88 char ch2 ;
99 // there is end-of-struct padding because of the int
10- } z1 ;
10+ };
11+
12+ STATIC_ASSERT (_Alignof(struct Z1 ) == _Alignof(int ));
13+
14+ #ifdef _MSC_VER
15+ STATIC_ASSERT (sizeof (struct Z1 ) == 4 + 4 + 4 );
16+ #else
17+ STATIC_ASSERT (sizeof (struct Z1 ) == 1 + 1 + 1 + 1 );
18+ #ifdef __GNUC__
19+ STATIC_ASSERT (__builtin_offsetof (struct Z1 , ch2 ) == 2 );
20+ #endif
21+ #endif
22+
23+ #pragma pack(push, 1)
24+ struct Z1_packed
25+ {
26+ char ch ;
27+ int i : 3 ;
28+ char ch2 ;
29+ // there is no end-of-struct padding
30+ };
31+ #pragma pack(pop)
32+
33+ STATIC_ASSERT (_Alignof(struct Z1_packed ) == 1 );
34+
35+ #ifdef _MSC_VER
36+ STATIC_ASSERT (sizeof (struct Z1_packed ) == 1 + 4 + 1 );
37+ #else
38+ STATIC_ASSERT (sizeof (struct Z1_packed ) == 1 + 1 + 1 );
39+ #ifdef __GNUC__
40+ STATIC_ASSERT (__builtin_offsetof (struct Z1_packed , ch2 ) == 2 );
41+ #endif
42+ #endif
1143
1244struct Z2
1345{
1446 char ch ;
1547 char i :3 ;
1648 char ch2 ;
1749 // there is no end-of-struct padding
18- } z2 ;
50+ };
51+
52+ STATIC_ASSERT (sizeof (struct Z2 ) == 1 + 1 + 1 );
53+
54+ #pragma pack(push, 1)
55+ struct Z2_packed
56+ {
57+ char ch ;
58+ char i : 3 ;
59+ char ch2 ;
60+ // there is no end-of-struct padding
61+ };
62+ #pragma pack(pop)
63+
64+ STATIC_ASSERT (sizeof (struct Z2_packed ) == 1 + 1 + 1 );
1965
2066struct Z3
2167{
2268 char ch ;
23- int i :3 ; // bit-fields do not get padding!
24- } z3 ;
69+ int i : 3 ; // padded by MSVC, not by gcc/clang
70+ };
71+
72+ #ifdef _MSC_VER
73+ STATIC_ASSERT (sizeof (struct Z3 ) == 4 + 4 );
74+ #else
75+ STATIC_ASSERT (sizeof (struct Z3 ) == 1 + 1 + 2 );
76+ #endif
77+
78+ #pragma pack(push, 1)
79+ struct Z3_packed
80+ {
81+ char ch ;
82+ int i : 3 ; // padded by MSVC, not by gcc/clang
83+ };
84+ #pragma pack(pop)
85+
86+ #ifdef _MSC_VER
87+ STATIC_ASSERT (sizeof (struct Z3_packed ) == 1 + 4 );
88+ #else
89+ STATIC_ASSERT (sizeof (struct Z3_packed ) == 1 + 1 );
90+ #endif
2591
2692struct Z4
2793{
2894 int i ;
2995 long long int x ; // pads to 8
3096 char ch ; // 7 end-of-struct padding
31- } z4 ;
97+ };
98+
99+ STATIC_ASSERT (sizeof (struct Z4 ) == 4 + 4 + 8 + 1 + 7 );
100+
101+ #pragma pack(push, 1)
102+ struct Z4_packed
103+ {
104+ int i ;
105+ long long int x ; // no padding
106+ char ch ; // no end-of-struct padding
107+ };
108+ #pragma pack(pop)
109+
110+ STATIC_ASSERT (sizeof (struct Z4_packed ) == 4 + 8 + 1 );
32111
33112struct Z5
34113{
35114 char ch ;
36115 long long int x []; // pads to 8, but has no size
37- } z5 ;
116+ };
38117
39- STATIC_ASSERT (sizeof (struct Z1 ) == 1 + 1 + 1 + 1 );
118+ STATIC_ASSERT (sizeof (struct Z5 ) == 8 );
40119
41- #ifdef __GNUC__
42- STATIC_ASSERT (__builtin_offsetof (struct Z1 , ch2 )== 2 );
120+ #pragma pack(push, 1)
121+ struct Z5_packed
122+ {
123+ char ch ;
124+ long long int x []; // pads to 8, but has no size
125+ };
126+ #pragma pack(pop)
127+
128+ STATIC_ASSERT (sizeof (struct Z5_packed ) == 1 );
129+
130+ struct Z6
131+ {
132+ char ch ;
133+ int i : 16 ; // padded to int by MSVC, to short by gcc/clang
134+ };
135+
136+ #ifdef _MSC_VER
137+ STATIC_ASSERT (sizeof (struct Z6 ) == 4 + 4 );
138+ #else
139+ STATIC_ASSERT (sizeof (struct Z6 ) == 1 + 1 + 2 );
43140#endif
44141
45- STATIC_ASSERT (sizeof (struct Z2 )== 1 + 1 + 1 );
46- STATIC_ASSERT (sizeof (struct Z3 )== 1 + 1 + 2 );
47- STATIC_ASSERT (sizeof (struct Z4 )== 4 + 4 + 8 + 1 + 7 );
48- STATIC_ASSERT (sizeof (struct Z5 )== 8 );
142+ #pragma pack(push, 1)
143+ struct Z6_packed
144+ {
145+ char ch ;
146+ int i : 16 ; // padded to int by MSC, but not aligned
147+ };
148+ #pragma pack(pop)
149+
150+ #ifdef _MSC_VER
151+ STATIC_ASSERT (sizeof (struct Z6_packed ) == 1 + 4 );
152+ #else
153+ STATIC_ASSERT (sizeof (struct Z6_packed ) == 1 + 2 );
154+ #endif
49155
50156int main ()
51157{
0 commit comments