1 #include <stddef.h>
2 #include <stdio.h>
3
4 //package size defaults to 8
5 struct E
6 {
7 int i; // size 4
8 short j; // size 2
9 double k; // size 8
10 };
11
12 #pragma pack(4)
13 struct F
14 {
15 int i;
16 short j;
17 double k;
18 };
19
20 #pragma pack(2)
21 struct T
22 {
23 int i;
24 short j;
25 double k;
26 };
27
28 struct struct_taga
29 {
30 char i;
31 short j;
32 };
33
34 #pragma pack(push, r2, 1)
35 struct struct_tagb
36 {
37 char i;
38 short j;
39 };
40
41 #pragma pack(push, 8)
42 struct struct_tagc
43 {
44 char i;
45 short j;
46 };
47
48
49 void main()
50 {
51 printf("%d ", offsetof(E, i));
52 printf("%d ", offsetof(E, j));
53 printf("%d\n", offsetof(E, k));
54 printf("%d ", offsetof(F, i));
55 printf("%d ", offsetof(F, j));
56 printf("%d\n", offsetof(F, k));
57 printf("%d ", offsetof(T, i));
58 printf("%d ", offsetof(T, j));
59 printf("%d\n", offsetof(T, k));
60
61 printf("sizeof(E) = %d\n", sizeof(E));
62 printf("sizeof(F) = %d\n", sizeof(F));
63 printf("sizeof(T) = %d\n", sizeof(T));
64
65 printf("sizeof(struct_taga) = %d\n", sizeof(struct_taga));
66 printf("sizeof(struct_tagb) = %d\n", sizeof(struct_tagb));
67 printf("sizeof(struct_tagc) = %d\n", sizeof(struct_tagc));
68 }
2 #include <stdio.h>
3
4 //package size defaults to 8
5 struct E
6 {
7 int i; // size 4
8 short j; // size 2
9 double k; // size 8
10 };
11
12 #pragma pack(4)
13 struct F
14 {
15 int i;
16 short j;
17 double k;
18 };
19
20 #pragma pack(2)
21 struct T
22 {
23 int i;
24 short j;
25 double k;
26 };
27
28 struct struct_taga
29 {
30 char i;
31 short j;
32 };
33
34 #pragma pack(push, r2, 1)
35 struct struct_tagb
36 {
37 char i;
38 short j;
39 };
40
41 #pragma pack(push, 8)
42 struct struct_tagc
43 {
44 char i;
45 short j;
46 };
47
48
49 void main()
50 {
51 printf("%d ", offsetof(E, i));
52 printf("%d ", offsetof(E, j));
53 printf("%d\n", offsetof(E, k));
54 printf("%d ", offsetof(F, i));
55 printf("%d ", offsetof(F, j));
56 printf("%d\n", offsetof(F, k));
57 printf("%d ", offsetof(T, i));
58 printf("%d ", offsetof(T, j));
59 printf("%d\n", offsetof(T, k));
60
61 printf("sizeof(E) = %d\n", sizeof(E));
62 printf("sizeof(F) = %d\n", sizeof(F));
63 printf("sizeof(T) = %d\n", sizeof(T));
64
65 printf("sizeof(struct_taga) = %d\n", sizeof(struct_taga));
66 printf("sizeof(struct_tagb) = %d\n", sizeof(struct_tagb));
67 printf("sizeof(struct_tagc) = %d\n", sizeof(struct_tagc));
68 }
0 4 8
0 4 8
0 4 6
sizeof(E) = 16
sizeof(F) = 16
sizeof(T) = 14
sizeof(struct_taga) = 4
sizeof(struct_tagb) = 3
sizeof(struct_tagc) = 4

浙公网安备 33010602011771号