title: C++ Data Member Alignment and Array Packing
body: <p>During a code review I've come across some code that defines a simple structure as follows:</p>
  
  <pre><code>class foo {
     unsigned char a;
     unsigned char b;
     unsigned char c;
  }
  </code></pre>
  
  <p>Elsewhere, an array of these objects is defined:</p>
  
  <pre><code>foo listOfFoos[SOME_NUM];
  </code></pre>
  
  <p>Later, the structures are raw-copied into a buffer:</p>
  
  <pre><code>memcpy(pBuff,listOfFoos,3*SOME_NUM);
  </code></pre>
  
  <p>This code relies on the assumptions that: a.) The size of foo is 3, and no padding is applied, and b.) An array of these objects is packed with no padding between them.</p>
  
  <p>I've tried it with GNU on two platforms (RedHat 64b, Solaris 9), and it worked on both.</p>
  
  <p>Are the assumptions above valid? If not, under what conditions (e.g. change in OS/compiler) might they fail?</p>