Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Alignment of member of structures (and classes) depends on platform, true, but on compiler too. The reason of align members to its size is for performance reason. Making all integral type aligned with its size reduce the memory access.</p> <p>You usually can force the compiler to reduce the alignment, but is not a good idea except for specific reasons (for example, for data compatibility between different platforms, as communication data). In Visual C++ exists <code>#pragma pack</code> for that, for example:</p> <pre><code>#pragma pack(1) struct ST1 { char c1; short s; char c2; double d; int i; }; assert(sizeof(ST1) == 16); </code></pre> <p>But as I said before is usually not a good idea.</p> <p>Keep in mind the compiler is not just adding pad bytes after some fields. It's also assuring the struct is allocated in memory for all fields are right aligned. I mean, in your ST1 sample, because the bigger field type is double, compiler will be assured <code>d</code> field will be aligned at 8 bytes (except if using <code>#pragma pack</code> or similar options):</p> <pre><code>ST1 st1; assert(&amp;st1.d % 8 == 0); </code></pre> <p>About your questions:</p> <ul> <li>If you want to save space, yes, is a good trick order fields by size, writing first the bigger. In case of composed structs, use the size of the bigger field of inner struct, instead the size of struct. </li> <li>It's working on standalone variables. But the compiler can order the variables in memory (as opposed as member of structs and classes).</li> </ul> <p>For example:</p> <pre><code>short s[27]; int32_t i32[34]; int64_t i64[45]; assert(s % 2 == 0); assert(i32 % 4 == 0); assert(i64 % 8 == 0); </code></pre> <ul> <li>Padding bytes can contains anything. Usually initialized data (at least you initialize it). Some times can contains specific byte pattern by compiler, for debugging reasons.</li> <li>About the structures with all members represented at memory by different addresses: sorry, I don't understand well what as you asking.</li> <li>The standard C++ says the address of a struct/class has to be the same of address of first field of such struct/class. Then, only is possible padding after <code>c3</code>, but never before <code>c1</code>.</li> </ul> <p>From N3337 (C++11) [9.2 class.menu, p.20]:</p> <blockquote> <p>A pointer to a standard-layout struct object, suitably converted using a <code>reinterpret_cast</code>, points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa. [ Note: There might therefore be unnamed padding within a standard-layout struct object, but not at its beginning, as necessary to achieve appropriate alignment. —end note ]</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload