-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.c
60 lines (52 loc) · 1.69 KB
/
example.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* example.c - Example of structure packing
*/
#include <stdio.h> // printf
#include <stddef.h> // offsetof
#include <stdlib.h> // exit
#include <stdbool.h>
int main(int argc, char *argv[])
{
struct start_pad {
char c; // 1 byte
char *p; // 8 bytes on a 32-bit machine
long l; // 8 bytes
} start;
printf("offsetof(start_pad, c) %i\n", offsetof(struct start_pad, c));
printf("offsetof(start_pad, p) %i\n", offsetof(struct start_pad, p));
printf("sizeof(start) %i\n", sizeof(start));
struct center_pad {
char *p; // 8 bytes on a 64-bit machine
char c; // 1 byte
long l; // 8 bytes
} center;
printf("offsetof(center_pad, p) %i\n", offsetof(struct center_pad, p));
printf("offsetof(center_pad, c) %i\n", offsetof(struct center_pad, c));
printf("sizeof(center) %i\n", sizeof(center));
struct {
char *p; // 8 bytes on a 64-bit machine
long l; // 8 bytes
char c; // 1 byte
} trailing;
printf("sizeof(trailing) %i\n", sizeof(trailing));
struct {
struct {
char *p; // 8 bytes on a 64-bit machine
} inner;
char c; // 1 byte
} nested;
printf("sizeof(nested) %i\n", sizeof(nested));
struct {
char c; // 1 byte
char *p; // 8 bytes on a 64-bit machine
short s; // 2 bytes
} unordered;
printf("sizeof(unordered) %i\n", sizeof(unordered));
struct {
char *p; // 8 bytes on a 64-bit machine
short s; // 2 bytes
char c; // 1 byte
} ordered;
printf("sizeof(ordered) %i\n", sizeof(ordered));
exit(EXIT_SUCCESS);
}