#include <cstdint>
#include <typeinfo>
#include <iostream>
template<typename T> void check_aligned(void *p) {
std::cout << p << " is " <<
(0==(reinterpret_cast<std::intptr_t>(p) % alignof(T))?"":"NOT ") <<
"aligned for the type " << typeid(T).name() << '\n';
}
void foo1() {
char a;
char b[sizeof (int)];
check_aligned<int>(b); // unaligned in clang
}
struct S {
char a;
char b[sizeof(int)];
};
void foo2() {
S s;
check_aligned<int>(s.b); // unaligned in clang and msvc
}
S s;
void foo3() {
check_aligned<int>(s.b); // unaligned in clang, msvc, and gcc
}
int main() {
foo1();
foo2();
foo3();
}
I2luY2x1ZGUgPGNzdGRpbnQ+CiNpbmNsdWRlIDx0eXBlaW5mbz4KI2luY2x1ZGUgPGlvc3RyZWFtPgoKdGVtcGxhdGU8dHlwZW5hbWUgVD4gdm9pZCBjaGVja19hbGlnbmVkKHZvaWQgKnApIHsKICAgIHN0ZDo6Y291dCA8PCBwIDw8ICIgaXMgIiA8PAogICAgICAoMD09KHJlaW50ZXJwcmV0X2Nhc3Q8c3RkOjppbnRwdHJfdD4ocCkgJSBhbGlnbm9mKFQpKT8iIjoiTk9UICIpIDw8CiAgICAgICJhbGlnbmVkIGZvciB0aGUgdHlwZSAiIDw8IHR5cGVpZChUKS5uYW1lKCkgPDwgJ1xuJzsKfQoKdm9pZCBmb28xKCkgewogICAgY2hhciBhOwogICAgY2hhciBiW3NpemVvZiAoaW50KV07CiAgICBjaGVja19hbGlnbmVkPGludD4oYik7IC8vIHVuYWxpZ25lZCBpbiBjbGFuZwp9CgpzdHJ1Y3QgUyB7CiAgICBjaGFyIGE7CiAgICBjaGFyIGJbc2l6ZW9mKGludCldOwp9OwoKdm9pZCBmb28yKCkgewogICAgUyBzOwogICAgY2hlY2tfYWxpZ25lZDxpbnQ+KHMuYik7IC8vIHVuYWxpZ25lZCBpbiBjbGFuZyBhbmQgbXN2Ywp9CgpTIHM7Cgp2b2lkIGZvbzMoKSB7CiAgICBjaGVja19hbGlnbmVkPGludD4ocy5iKTsgLy8gdW5hbGlnbmVkIGluIGNsYW5nLCBtc3ZjLCBhbmQgZ2NjCn0KCmludCBtYWluKCkgewogICAgZm9vMSgpOwogICAgZm9vMigpOwogICAgZm9vMygpOwp9Cg==