-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path04-permutate-layout.cpp
More file actions
106 lines (86 loc) · 2.36 KB
/
04-permutate-layout.cpp
File metadata and controls
106 lines (86 loc) · 2.36 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Find out the memory layout of a class
Do the order affect the class?
Compile:
(GCC)
$ g++ permutate-layout.cpp -std=c++11 -o permutate-layout
*/
#include "util.hpp"
#include <cstddef>
/*
Observe the address of each of them.
Question:
- Is 4 the only alignment for struct in memory?
- If not, what else?
- What is the content of padding? Random or some value?
- How can you confirm it?
Conclusion:
Placing variables in certain orders will affect the size. Each members variable of struct or class
should be always aligned therefore compiler will give a padding if neessary
*/
//======== Type Definitions =========================================
// Size: 16 = 4 + 10 + 2
struct Example1
{
int32_t x;
int8_t y[10];
int16_t z;
};
// Size: 16 = 4 + 2 + 10
struct Example2
{
int32_t x;
int16_t y;
int8_t z[10];
};
// Size: 20 = 10 (+2 padding) + 4 + 2, the first 10 is forced to rount to multiple of 4
struct Example3
{
int8_t x[10];
int32_t y;
int16_t z;
};
// Size: 16 = 1 + 8 then round to multiple of 8
struct Example4
{
int8_t x;
int64_t y;
};
// Size: 8 = 4 + 1 then round to multiple of 4
struct Example5
{
int32_t x;
int8_t y;
};
//======== Helper Functions =========================================
template<typename T>
void dump_variables(T &t);
//======== Main Function ============================================
int main()
{
Example1 ex1;
Example2 ex2;
Example3 ex3;
// A trick to print offset:
// (int) offsetof(ClassName,field)
dump_instance("Instance of Example1", ex1);
dump_variables(ex1);
printf("\n");
dump_instance("Instance of Example2", ex2);
dump_variables(ex2);
printf("\n");
dump_instance("Instance of Example3", ex3);
dump_variables(ex3);
printf("\n");
printf("Sizeof(Example4) = %d\n", sizeof(Example4));
printf("Sizeof(Example5) = %d\n", sizeof(Example5));
return 0;
}
//======== Implementations ==========================================
template<typename T>
void dump_variables(T &t)
{
printf("Address of x: [%p] +%d\n", &t.x, (size_t)offsetof(T,x));
printf("Address of y: [%p] +%d\n", &t.y, (size_t)offsetof(T,y));
printf("Address of z: [%p] +%d\n", &t.z, (size_t)offsetof(T,z));
}