-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalignment.cpp
More file actions
86 lines (67 loc) · 2.69 KB
/
alignment.cpp
File metadata and controls
86 lines (67 loc) · 2.69 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
#include<iostream>
#include<memory>
#include<ios>
struct S1 {
char c1;
int d1;
char c2;
int d2;
void set_default() {
c1 = 'a';
c2 = 'b';
d1 = 1;
d2 = 2;
}
void print() {
std::cout << "size:" << sizeof(c1) << "\t" << "align:" << alignof(c1) << "\n";
std::cout << "c1:\t" << c1 << "\t" << (int*) &c1 << "\n";
std::cout << "size:" << sizeof(d1) << "\t" << "align:" << alignof(d1) << "\n";
std::cout << "d1:\t" << d1 << "\t" << &d1 << "\n";
std::cout << "size:" << sizeof(c2) << "\t" << "align:" << alignof(c2) << "\n";
std::cout << "c2:\t" << c2 << "\t" << (int*) &c2 << "\n";
std::cout << "size:" << sizeof(d2) << "\t" << "align:" << alignof(d2) << "\n";
std::cout << "d2:\t" << d2 << "\t" << &d2 << "\n\n";
}
};
struct S2 {
char c1;
char c2;
int d1;
int d2;
void set_default() {
c1 = 'a';
c2 = 'b';
d1 = 1;
d2 = 2;
}
void print() {
std::cout << "size:" << sizeof(c1) << "\t" << "align:" << alignof(c1) << "\n";
std::cout << "c1:\t" << c1 << "\t" << (int*) &c1 << "\n";
std::cout << "size:" << sizeof(c2) << "\t" << "align:" << alignof(c2) << "\n";
std::cout << "c2:\t" << c2 << "\t" << (int*) &c2 << "\n";
std::cout << "size:" << sizeof(d1) << "\t" << "align:" << alignof(d1) << "\n";
std::cout << "d1:\t" << d1 << "\t" << &d1 << "\n";
std::cout << "size:" << sizeof(d2) << "\t" << "align:" << alignof(d2) << "\n";
std::cout << "d2:\t" << d2 << "\t" << &d2 << "\n\n";
}
};
int main() {
S1 s1 = S1();
S2 s2 = S2();
int x1 = 1;
int x2 = 2;
alignas(8) int x3 = 3;
std::cout << "size:" << sizeof(s1) << "\t" << "align:" << alignof(s1) << "\n";
s1.set_default();
s1.print();
std::cout << "size:" << sizeof(s2) << "\t" << "align:" << alignof(s2) << "\n";
s2.set_default();
s2.print();
std::cout << "size:" << sizeof(x1) << "\t" << "align:" << alignof(x1) << "\n";
std::cout << "size:" << sizeof(x2) << "\t" << "align:" << alignof(x2) << "\n";
std::cout << "size:" << sizeof(x3) << "\t" << "align:" << alignof(x3) << "\n";
std::cout << "x1:\t" << x1 << "\t" << &x1 << "\n";
std::cout << "x2:\t" << x2 << "\t" << &x2 << "\n";
std::cout << "x3:\t" << x3 << "\t" << &x3 << "\n\n";
return 0;
}