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
61
62
63
64
65
|
#include <stdio.h>
#include <stdint.h>
typedef struct {
int8_t c1;
int8_t v;
} struct_alignment_int8;
typedef struct {
int8_t c1;
int16_t v;
} struct_alignment_int16;
typedef struct {
int8_t c1;
int32_t v;
} struct_alignment_int32;
typedef struct {
int8_t c1;
int64_t v;
} struct_alignment_int64;
typedef struct {
int8_t c1;
long v;
} struct_alignment_long;
typedef struct {
int8_t c1;
void * v;
} struct_alignment_pointer;
typedef struct {
int8_t c1;
float v;
} struct_alignment_float;
typedef struct {
int8_t c1;
double v;
} struct_alignment_double;
typedef struct {
char c1;
long double v;
} struct_alignment_longdouble;
size_t padding(size_t totalsize, size_t typesize) { return totalsize - typesize - sizeof(char); }
size_t alignment(size_t totalsize, size_t typesize) { return totalsize - typesize; }
int main(int argc, char * argv[] ) {
printf("sizeof/alignment int8_t: %d / %d / %d\n", sizeof(int8_t), sizeof( struct_alignment_int8 ), alignment(sizeof( struct_alignment_int8 ), sizeof(int8_t)));
printf("sizeof/alignment int16_t: %d / %d / %d\n", sizeof(int16_t), sizeof( struct_alignment_int16 ), alignment(sizeof( struct_alignment_int16 ), sizeof(int16_t)));
printf("sizeof/alignment int32_t: %d / %d / %d\n", sizeof(int32_t), sizeof( struct_alignment_int32 ), alignment(sizeof( struct_alignment_int32 ), sizeof(int32_t)));
printf("sizeof/alignment int64_t: %d / %d / %d\n", sizeof(int64_t), sizeof( struct_alignment_int64 ), alignment(sizeof( struct_alignment_int64 ), sizeof(int64_t)));
printf("sizeof/alignment long: %d / %d / %d\n", sizeof(long), sizeof( struct_alignment_long ), alignment(sizeof( struct_alignment_long ), sizeof(long)));
printf("sizeof/alignment pointer: %d / %d / %d\n", sizeof(void *), sizeof( struct_alignment_pointer ), alignment(sizeof( struct_alignment_pointer ), sizeof(void *)));
printf("sizeof/alignment float: %d / %d / %d\n", sizeof(float), sizeof( struct_alignment_float ), alignment(sizeof( struct_alignment_float ), sizeof(float)));
printf("sizeof/alignment double: %d / %d / %d\n", sizeof(double), sizeof( struct_alignment_double ), alignment(sizeof( struct_alignment_double ), sizeof(double)));
printf("sizeof/alignment long double: %d / %d / %d\n", sizeof(long double), sizeof( struct_alignment_longdouble ), alignment(sizeof( struct_alignment_longdouble ), sizeof(long double)));
return 0;
}
|