forked from thradams/cake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cCode117.c
29 lines (26 loc) · 886 Bytes
/
cCode117.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
//en.cppreference.com/w/c/language/object.html
#include <stdalign.h>
#include <stdio.h>
// objects of struct S can be allocated at any address
// because both S.a and S.b can be allocated at any address
struct S
{
char a; // size: 1, alignment: 1
char b; // size: 1, alignment: 1
}; // size: 2, alignment: 1
// objects of struct X must be allocated at 4-byte boundaries
// because X.n must be allocated at 4-byte boundaries
// because int's alignment requirement is (usually) 4
struct X
{
int n; // size: 4, alignment: 4
char c; // size: 1, alignment: 1
// three bytes padding
}; // size: 8, alignment: 4
int main(void)
{
printf("sizeof(struct S) = %zu\n", sizeof(struct S));
printf("alignof(struct S) = %zu\n", alignof(struct S));
printf("sizeof(struct X) = %zu\n", sizeof(struct X));
printf("alignof(struct X) = %zu\n", alignof(struct X));
}