Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In most architecture, the underlying access must match the alignment of the accessed data.</p> <p>This mean, that if you have a 32 bit value, you can access it efficiently if it is stored at an address that is dividable by four.</p> <p>If you use <code>#pragma pack</code>, the location of the variable can be anything, and the compiler must access the element piece by piece and combine them together. Concretely, below is the generated code to read a normal <code>int</code> on a V850E (a popular microcontoller in the embedded world):</p> <pre><code>LD.W a[zero],r5 </code></pre> <p>Correspondingly, the following is the code to access an <code>int</code> in a packed structure:</p> <pre><code>LD.BU g+3[zero],r1 SHL 8,r1 LD.BU g+2[zero],r6 OR r1,r6 SHL 8,r6 LD.BU g+1[zero],r7 OR r6,r7 SHL 8,r7 LD.BU g[zero],r1 OR r7,r1 </code></pre> <p>Another reason not to use packed structs is that it's not possible to dereference a pointer to a member of a packed struct, unless the architecture support unaligned pointer accesses. The reason for this is that type of the point will be a plain <code>int</code> pointer, and the compiler has no knowledge that it must access whatever it points to piece-by-piece.</p> <p>I would strongly recommend that you don't use '#pragma pack' at all, unless it's absolutely necessary. If you have control over the struct definition, there are techniques to make sure the struct layout is padding-free. If not, a better approach would be to copy any unaligned data to a new, aligned, struct and use it in your application.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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