Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is because of padding added to satisfy alignment constraints. <a href="http://en.wikipedia.org/wiki/Data_structure_alignment" rel="noreferrer">Data structure alignment</a> impacts both performance and correctness of programs:</p> <ul> <li>Mis-aligned access might be a hard error (often <code>SIGBUS</code>).</li> <li>Mis-aligned access might be a soft error. <ul> <li>Either corrected in hardware, for a modest performance-degradation.</li> <li>Or corrected by emulation in software, for a severe performance-degradation.</li> <li>In addition, atomicity and other concurrency-guarantees might be broken, leading to subtle errors.</li> </ul></li> </ul> <p>Here's an example using typical settings for an x86 processor (all used 32 and 64 bit modes):</p> <pre><code>struct X { short s; /* 2 bytes */ /* 2 padding bytes */ int i; /* 4 bytes */ char c; /* 1 byte */ /* 3 padding bytes */ }; struct Y { int i; /* 4 bytes */ char c; /* 1 byte */ /* 1 padding byte */ short s; /* 2 bytes */ }; struct Z { int i; /* 4 bytes */ short s; /* 2 bytes */ char c; /* 1 byte */ /* 1 padding byte */ }; const int sizeX = sizeof(struct X); /* = 12 */ const int sizeY = sizeof(struct Y); /* = 8 */ const int sizeZ = sizeof(struct Z); /* = 8 */ </code></pre> <p>One can minimize the size of structures by sorting members by alignment (sorting by size suffices for that in basic types) (like structure <code>Z</code> in the example above).</p> <p>IMPORTANT NOTE: Both the C and C++ standards state that structure alignment is implementation-defined. Therefore each compiler may choose to align data differently, resulting in different and incompatible data layouts. For this reason, when dealing with libraries that will be used by different compilers, it is important to understand how the compilers align data. Some compilers have command-line settings and/or special <code>#pragma</code> statements to change the structure alignment settings.</p>
 

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