fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #define ALIGNOF(type) ((int)(offsetof(struct {char c; type t;}, t)))
  5.  
  6. union ull {
  7. unsigned long long m;
  8. };
  9.  
  10. struct sll {
  11. unsigned long long m;
  12. };
  13.  
  14. struct sull {
  15. ull m;
  16. };
  17.  
  18. union ulla {
  19. unsigned long long m;
  20. } __attribute__ ((aligned(8)));
  21.  
  22. struct slla {
  23. unsigned long long m;
  24. };
  25.  
  26. struct sulla {
  27. ulla m;
  28. } __attribute__ ((aligned(8)));;
  29.  
  30. long long a;
  31. char b;
  32. long long c;
  33. char d;
  34. ull e;
  35.  
  36.  
  37. int main() {
  38. #define pr(v) cout << #v ": " << (v) << endl
  39. #define prt(t) pr(sizeof(t)); pr(__alignof__(t)); pr(ALIGNOF(t))
  40. prt(unsigned long long);
  41. prt(ull);
  42. prt(ull);
  43. prt(sll);
  44. prt(ulla);
  45. prt(ulla);
  46. prt(slla);
  47. // test real alignment of long long
  48. pr(size_t((void*)&a));
  49. pr(size_t((void*)&b));
  50. pr(size_t((void*)&c));
  51. pr(size_t((void*)&d));
  52. pr(size_t((void*)&e));
  53. pr(size_t((void*)&c) - size_t((void*)&b));
  54. pr(size_t((void*)&e) - size_t((void*)&d));
  55.  
  56. { // test local objects
  57. long long a;
  58. char b;
  59. long long c;
  60. char d;
  61. ull e;
  62. pr(size_t((void*)&a));
  63. pr(size_t((void*)&b));
  64. pr(size_t((void*)&c));
  65. pr(size_t((void*)&d));
  66. pr(size_t((void*)&e));
  67. pr(size_t((void*)&c) - size_t((void*)&b));
  68. pr(size_t((void*)&e) - size_t((void*)&d));
  69. }
  70. };
Success #stdin #stdout 0s 2888KB
stdin
Standard input is empty
stdout
sizeof(unsigned long long): 8
__alignof__(unsigned long long): 8
ALIGNOF(unsigned long long): 4
sizeof(ull): 8
__alignof__(ull): 4
ALIGNOF(ull): 4
sizeof(ull): 8
__alignof__(ull): 4
ALIGNOF(ull): 4
sizeof(sll): 8
__alignof__(sll): 4
ALIGNOF(sll): 4
sizeof(ulla): 8
__alignof__(ulla): 8
ALIGNOF(ulla): 8
sizeof(ulla): 8
__alignof__(ulla): 8
ALIGNOF(ulla): 8
sizeof(slla): 8
__alignof__(slla): 4
ALIGNOF(slla): 4
size_t((void*)&a): 134524856
size_t((void*)&b): 134524864
size_t((void*)&c): 134524872
size_t((void*)&d): 134524880
size_t((void*)&e): 134524884
size_t((void*)&c) - size_t((void*)&b): 8
size_t((void*)&e) - size_t((void*)&d): 4
size_t((void*)&a): 3219994112
size_t((void*)&b): 3219994127
size_t((void*)&c): 3219994104
size_t((void*)&d): 3219994126
size_t((void*)&e): 3219994096
size_t((void*)&c) - size_t((void*)&b): 4294967273
size_t((void*)&e) - size_t((void*)&d): 4294967266