Data Structure Alignment and Padding in C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Coundin云英 面试
#include <stdio.h>

typedef struct
{
char c;
int i;
}T_FOO;

typedef struct
{
int i;
char c;
}U_FOO;

int main()
{
printf("sizeof(char) = %ld\n", sizeof(char));
printf("sizeof(int) = %ld\n", sizeof(int));
printf("sizeof(long) = %ld\n", sizeof(long));

T_FOO a;
U_FOO b;

printf("a.c -> %ld, a.i -> %ld\n", (void *)&a.c - (void *)&a, (void *)&a.i - (void *)&a);
printf("b.i -> %ld, b.c -> %ld\n", (void *)&b.i - (void *)&b, (void *)&b.c - (void *)&b);

printf("sizeof(a) = %ld\n", sizeof(a));
printf("sizeof(b) = %ld\n", sizeof(b));
return 0;
}