title: Is the size of a struct required to be an exact multiple of the alignment of that struct?
body: <p>Once again, I'm questioning a longstanding belief.</p>
  
  <p>Until today, I believed that the alignment of the following struct would normally be 4 and the size would normally be 5...</p>
  
  <pre><code>struct example
  {
    int   m_Assume_32_Bits;
    char  m_Assume_8_Bit_Bytes;
  };
  </code></pre>
  
  <p>Because of this assumption, I have data structure code that uses offsetof to determine the distance in bytes between two adjacent items in an array. Today, I spotted some old code that was using sizeof where it shouldn't, couldn't understand why I hadn't had bugs from it, coded up a unit test - and the test surprised me by passing.</p>
  
  <p>A bit of investigation showed that the sizeof the type I used for the test (similar to the struct above) was an exact multiple of the alignment - ie 8 bytes. It had padding after the final member. Here is an example of why I never expected this...</p>
  
  <pre><code>struct example2
  {
    example m_Example;
    char    m_Why_Cant_This_Be_At_Offset_6_Bytes;
  };
  </code></pre>
  
  <p>A bit of Googling showed examples that make it clear that this padding after the final member is allowed - for example <a href="http://en.wikipedia.org/wiki/Data_structure_alignment#Data_structure_padding" rel="noreferrer">http://en.wikipedia.org/wiki/Data_structure_alignment#Data_structure_padding</a> (the "or at the end of the structure" bit).</p>
  
  <p>This is a bit embarrassing, as I recently posted this comment - <a href="https://stackoverflow.com/questions/4587470/use-of-struct-padding/4587489#4587489">Use of struct padding</a> (my first comment to that answer).</p>
  
  <p>What I can't seem to determine is whether this padding to an exact multiple of the alignment is guaranteed by the C++ standard, or whether it is just something that is permitted and that some (but maybe not all) compilers do.</p>
  
  <p>So - is the size of a struct required to be an exact multiple of the alignment of that struct according to the C++ standard?</p>
  
  <p>If the C standard makes different guarantees, I'm interested in that too, but the focus is on C++.</p>