Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Firmware developer here. <code>#pragma pack</code> is very familiar territory. I'll explain.</p> <p>In general you <strong>should not</strong> use <code>#pragma pack</code>. Yes, it will make your structures smaller in memory since it eliminates all padding between struct members. But it can make <strong><em>accessing</em></strong> those members much more expensive since the members may no longer fall along their required alignment. For example, in ARM architectures, 4-byte ints are typically required to be 4-byte aligned, but in a packed struct they might not be. That means the compiler needs to add extra instructions to safely access that struct member, or the developer has to access it byte-by-byte and reconstruct the int manually. Either way it results in more code than an aligned access, so your struct ends up smaller but your accessing code potentially ends up slower and larger.</p> <p>You <strong>should</strong> use <code>#pragma pack</code> when your structure must match an <strong>exact</strong> data layout. This typically happens when you are writing code to match a data transport or access specification... e.g., network protocols, storage protocols, device drivers that access HW registers. In those cases you may need <code>#pragma pack</code> to force your structures to match the spec-defined data layout. This will possibly incur the same performance penalty mentioned in the previous paragraph, but may be the only way to comply with the specification.</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