title: Figuring out the size of a struct in C (data alignment issue)
body: <p>Consider the following code segment:</p>
  
  <pre><code>struct MixedData {
      char a;
      short b;
      char c;
      long d;
      char e;
  };
  
  int main() {
      cout&lt;&lt;sizeof(MixedData)&lt;&lt;endl;
  }
  </code></pre>
  
  <p>(Ignore the C++ syntax, since it is not the issue here)<br>
  The output for this code is 24, and I don't quite understand why.<br>
  I'll denote:<br>
  <strong>C</strong>: char<br>
  <strong>S</strong>: short<br>
  <strong>L</strong>: long<br>
  <strong>P</strong>: compiler padding<br>
  and each | | is one byte.<br>
  to my best understanding, the data alignment is:  </p>
  
  <pre><code>|C|P|S|S|C|P|P|P|L|L|L|L|L|L|L|L|C|P|P|P|
  </code></pre>
  
  <p>so the output should be 20, shouldn't it?
  the funny thing is that when MixedData becomes:</p>
  
  <pre><code>struct MixedData {
      char a;
      short b;
      char c;
      int d;
      char e;
  };
  </code></pre>
  
  <p>the size becomes 16, as it should.<br>
  Does anyone have an explanation for that?  </p>