title: x86_64 stack alignment - purpose of excessive bytes
body: <p>Recently I was learning about the topic of memory alignment and related issues and it led me to a following program:</p>
  
  <pre><code>#include &lt;cstdio&gt;
  #include &lt;cstdint&gt;
  
  
  struct XX
  {
      uint8_t a;
      uint32_t b;
      uint16_t d;
      uint64_t c;
  };
  
  int main()
  {
       printf("\n\ntype size: %zu\n", sizeof(XX));
  
       XX one;
       XX two;
       XX three;
       printf("addresses of one-three:\n\t%p\n\t%p\n\t%p\n", reinterpret_cast&lt;void *&gt;(&amp;one), reinterpret_cast&lt;void *&gt;(&amp;two), reinterpret_cast&lt;void *&gt;(&amp;three));
       printf("\ndifference of addresses between one and two: %lu\n", reinterpret_cast&lt;unsigned long&gt;(&amp;one) - reinterpret_cast&lt;unsigned long&gt;(&amp;two));
       printf("difference of addresses between two and three: %lu\n", reinterpret_cast&lt;unsigned long&gt;(&amp;two) - reinterpret_cast&lt;unsigned long&gt;(&amp;three));
       printf("alignment of type alone: %zu\n", alignof(XX));
  
       XX arr[2];
       printf("\ndifference of addresses in array: %lu\n", reinterpret_cast&lt;unsigned long&gt;(&amp;arr[1]) - reinterpret_cast&lt;unsigned long&gt;(&amp;arr[0]));
       printf("alignment of array type: %zu\n", alignof(XX[]));
  }
  </code></pre>
  
  <p>I compiled this with GCC 8.1.0 as:</p>
  
  <blockquote>
    <p>g++ -std=c++17 -O0 main.cpp</p>
  </blockquote>
  
  <p>The ouput says that:</p>
  
  <ul>
  <li>alignment is 8-byte,</li>
  <li>size is 24 bytes,</li>
  <li><em>XX</em> instances differ by 24 bytes inside the array (contiguous memory - no surprise), <strong>but by 32 bytes as free-standing variables</strong>.</li>
  </ul>
  
  <p>Why are there excessive 8 bytes between the free-standing variables?</p>