book: add memory alignment alignof and alignas

see #2
This commit is contained in:
Changkun Ou
2019-07-19 12:00:46 +02:00
parent dae42fccde
commit 115827f98c
5 changed files with 91 additions and 2 deletions

21
code/9/9.3.alignment.cpp Normal file
View File

@@ -0,0 +1,21 @@
#include <iostream>
struct Storage {
char a;
int b;
double c;
long long d;
};
struct alignas(std::max_align_t) AlignasStorage {
char a;
int b;
double c;
long long d;
};
int main() {
std::cout << alignof(Storage) << std::endl;
std::cout << alignof(AlignasStorage) << std::endl;
return 0;
}