title: Determining the alignment of C/C++ structures in relation to its members
body: <p>Can the alignment of a structure type be found if the alignments of the structure members are known? </p>
  
  <p>Eg. for:</p>
  
  <pre><code>struct S
  {
   a_t a;
   b_t b;
   c_t c[];
  };
  </code></pre>
  
  <p>is the alignment of S = max(alignment_of(a), alignment_of(b), alignment_of(c))?</p>
  
  <p>Searching the internet I found that "for structured types the largest alignment requirement of any of its elements determines the alignment of the structure" (in <a href="http://people.redhat.com/drepper/cpumemory.pdf" rel="noreferrer">What Every Programmer Should Know About Memory</a>) but I couldn't find anything remotely similar in the standard (latest draft more exactly). </p>
  
  <hr>
  
  <p><strong>Edited:</strong>
  Many thanks for all the answers, especially to Robert Gamble who provided a really good answer to the original question and the others who contributed.</p>
  
  <p>In short:</p>
  
  <p><strong><em>To ensure alignment requirements for structure members, the alignment of a structure must be at least as strict as the alignment of its strictest member.</em></strong></p>
  
  <p>As for determining the alignment of structure a few options were presented and with a bit of research this is what I found:</p>
  
  <ul>
  <li>c++ std::tr1::alignment_of
  
  <ul>
  <li>not standard yet, but close (technical report 1), should be in the C++0x</li>
  <li>the following restrictions are present in the latest draft: Precondition:T shall be a complete type, a reference type, or an array of
  unknown bound, but shall not be a function type or (possibly
  cv-qualified) void.
  
  <ul>
  <li>this means that my presented use case with the C99 flexible array won't work (this is not that surprising since flexible arrays are not standard c++) </li>
  </ul></li>
  <li>in the latest c++ draft it is defined in the terms of a new keyword - alignas (this has the same complete type requirement)</li>
  <li>in my opinion, should c++ standard ever support C99 flexible arrays, the requirement could be relaxed (the alignment of the structure with the flexible array should not change based on the number of the array elements)</li>
  </ul></li>
  <li>c++ boost::alignment_of 
  
  <ul>
  <li>mostly a tr1 replacement</li>
  <li>seems to be specialized for void and returns 0 in that case (this is forbidden in the c++ draft)</li>
  <li>Note from developers: strictly speaking you should only rely on the value of ALIGNOF(T) being a multiple of the true alignment of T, although in practice it does compute the correct value in all the cases we know about.</li>
  <li>I don't know if this works with flexible arrays, it should (might not work in general, this resolves to compiler intrinsic on my platform so I don't know how it will behave in the general case)</li>
  </ul></li>
  <li>Andrew Top presented a simple template solution for calculating the alignment in the answers
  
  <ul>
  <li>this seems to be very close to what boost is doing (boost will additionally return the object size as the alignment if it is smaller than the calculated alignment as far as I can see) so probably the same notice applies</li>
  <li>this works with flexible arrays</li>
  </ul></li>
  <li>use Windbg.exe to find out the alignment of a symbol
  
  <ul>
  <li>not compile time, compiler specific, didn't test it</li>
  </ul></li>
  <li>using offsetof on the anonymous structure containing the type
  
  <ul>
  <li>see the answers, not reliable, not portable with c++ non-POD</li>
  </ul></li>
  <li>compiler intrinsics, eg. MSVC __alignof
  
  <ul>
  <li>works with flexible arrays</li>
  <li>alignof keyword is in the latest c++ draft</li>
  </ul></li>
  </ul>
  
  <hr>
  
  <p>If we want to use the "standard" solution we're limited to std::tr1::alignment_of, but that won't work if you mix your c++ code with c99's flexible arrays.</p>
  
  <p>As I see it there is only 1 solution - use the old struct hack: </p>
  
  <pre><code>struct S
  {
   a_t a;
   b_t b;
   c_t c[1]; // "has" more than 1 member, strictly speaking this is undefined behavior in both c and c++ when used this way
  };
  </code></pre>
  
  <p>The diverging c and c++ standards and their growing differences are unfortunate in this case (and every other case).</p>
  
  <hr>
  
  <p>Another interesting question is (if we can't find out the alignment of a structure in a portable way) what is the most strictest alignment requirement possible. There are a couple of solutions I could find:</p>
  
  <ul>
  <li>boost (internally) uses a union of variety of types and uses the boost::alignment_of on it</li>
  <li>the latest c++ draft contains std::aligned_storage
  
  <ul>
  <li>The value of default-alignment shall be the most stringent alignment requirement for any C++ object type whose size is no greater than Len
  
  <ul>
  <li>so the <code>std::alignment_of&lt; std::aligned_storage&lt;BigEnoughNumber&gt;&gt;::value</code> should give us the maximum alignment  </li>
  <li>draft only, not standard yet (if ever), <code>tr1::aligned_storage</code> does not have this property</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>Any thoughts on this would also be appreciated. </p>
  
  <p><em>I have temporarily unchecked the accepted answer to get more visibility and input on the new sub-questions</em></p>