Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Packing and byte alignment, as described in the C FAQ <a href="http://www.c-faq.com/struct/align.html" rel="noreferrer">here</a>:</p> <blockquote> <p>It's for alignment. Many processors can't access 2- and 4-byte quantities (e.g. ints and long ints) if they're crammed in every-which-way.</p> <p>Suppose you have this structure:</p> <pre><code>struct { char a[3]; short int b; long int c; char d[3]; }; </code></pre> <p>Now, you might think that it ought to be possible to pack this structure into memory like this:</p> <pre><code>+-------+-------+-------+-------+ | a | b | +-------+-------+-------+-------+ | b | c | +-------+-------+-------+-------+ | c | d | +-------+-------+-------+-------+ </code></pre> <p>But it's much, much easier on the processor if the compiler arranges it like this:</p> <pre><code>+-------+-------+-------+ | a | +-------+-------+-------+ | b | +-------+-------+-------+-------+ | c | +-------+-------+-------+-------+ | d | +-------+-------+-------+ </code></pre> <p>In the packed version, notice how it's at least a little bit hard for you and me to see how the b and c fields wrap around? In a nutshell, it's hard for the processor, too. Therefore, most compilers will pad the structure (as if with extra, invisible fields) like this:</p> <pre><code>+-------+-------+-------+-------+ | a | pad1 | +-------+-------+-------+-------+ | b | pad2 | +-------+-------+-------+-------+ | c | +-------+-------+-------+-------+ | d | pad3 | +-------+-------+-------+-------+ </code></pre> </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