title: How should #pragma pack(8) work?
body: <p>I'm new to structure alignment and packing. I thought I understood it, but I'm finding some results that I did not expect (see below).</p>
  
  <p>My understanding of structure alignment is:</p>
  
  <ol>
  <li><p>Types are generally aligned on memory addresses that are multiples of their size.</p></li>
  <li><p>Padding is added as needed to facilitate proper alignment</p></li>
  <li><p>The end of a structure must be padded out to a multiple of the largest element (to facilitate array access)</p></li>
  </ol>
  
  <p>The <code>#pragma pack</code> directive basically allows overriding the general convention of aligning based on a type's size:</p>
  
  <pre><code>#pragma pack(push, 8)
  struct SPack8
  {
    // Assume short int is 2 bytes, double is 8 bytes, and int is 4 bytes
    short int a;
    double b;
    short int c;
    int d;
  };
  #pragma pack(pop)
  
  Pseudo struct layout: What I expected:
  // note: PADDING IS BRACKETED
  0, 1, [2, 3, 4, 5, 6, 7] // a occupies address 0, 1
  8, 9, 10, 11, 12, 13, 14, 15, // b occupies 8-15 inclusive
  16, 17, [18, 19, 20, 21, 22, 23] // c occupies 16-17 inclusive
  24, 25, 26, 27 // d occupies 24-27 inclusive
  // Thus far, SPack8 is 28 bytes, but the structure must be a multiple of
  // sizeof(double) so we need to add padding to make it 32 bytes
  [28, 29, 30, 31]
  </code></pre>
  
  <p>To my surprise, sizeof(SPack8) == 24 on VS 2015 x86. It seems that d is not being aligned on an 8 byte address:</p>
  
  <pre><code>offsetof(SPack, a) // 0, as expected
  offsetof(SPack, b) // 8, as expected
  offsetof(Spack, c) // 16, as expected
  offsetof(SPack, d) // 20..what??
  </code></pre>
  
  <p>Can someone please explain what is happening/what I have misunderstood?</p>
  
  <p>Thank you!</p>