@@ -7,45 +7,151 @@ struct Z1
7
7
int i :3 ;
8
8
char ch2 ;
9
9
// 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
11
43
12
44
struct Z2
13
45
{
14
46
char ch ;
15
47
char i :3 ;
16
48
char ch2 ;
17
49
// 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 );
19
65
20
66
struct Z3
21
67
{
22
68
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
25
91
26
92
struct Z4
27
93
{
28
94
int i ;
29
95
long long int x ; // pads to 8
30
96
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 );
32
111
33
112
struct Z5
34
113
{
35
114
char ch ;
36
115
long long int x []; // pads to 8, but has no size
37
- } z5 ;
116
+ };
38
117
39
- STATIC_ASSERT (sizeof (struct Z1 ) == 1 + 1 + 1 + 1 );
118
+ STATIC_ASSERT (sizeof (struct Z5 ) == 8 );
40
119
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 );
43
140
#endif
44
141
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
49
155
50
156
int main ()
51
157
{
0 commit comments