fork(8) download
  1. #include <cstdint>
  2. #include <typeinfo>
  3. #include <iostream>
  4.  
  5. template<typename T> void check_aligned(void *p) {
  6. std::cout << p << " is " <<
  7. (0==(reinterpret_cast<std::intptr_t>(p) % alignof(T))?"":"NOT ") <<
  8. "aligned for the type " << typeid(T).name() << '\n';
  9. }
  10.  
  11. void foo1() {
  12. char a;
  13. char b[sizeof (int)];
  14. check_aligned<int>(b); // unaligned in clang
  15. }
  16.  
  17. struct S {
  18. char a;
  19. char b[sizeof(int)];
  20. };
  21.  
  22. void foo2() {
  23. S s;
  24. check_aligned<int>(s.b); // unaligned in clang and msvc
  25. }
  26.  
  27. S s;
  28.  
  29. void foo3() {
  30. check_aligned<int>(s.b); // unaligned in clang, msvc, and gcc
  31. }
  32.  
  33. int main() {
  34. foo1();
  35. foo2();
  36. foo3();
  37. }
  38.  
Success #stdin #stdout 0s 2928KB
stdin
Standard input is empty
stdout
0xbfc5f59c is aligned for the type i
0xbfc5f59c is aligned for the type i
0x8049c61 is NOT aligned for the type i