Open main menu

Difference between revisions of "Coding Conventions"

→‎Struct packing: -- code highlighting
m (Fix code highlighting)
(→‎Struct packing: -- code highlighting)
Line 29: Line 29:
Do *not* assume that structs are stored in a certain way in memory -- the layout of a struct in memory can vary a lot between platforms, and on most modern systems it will almost never be "packed". If you absolutely must use packed structs, do not just use some #pragma or __attribute__ (as that is not portable either). Instead, do the following:
Do *not* assume that structs are stored in a certain way in memory -- the layout of a struct in memory can vary a lot between platforms, and on most modern systems it will almost never be "packed". If you absolutely must use packed structs, do not just use some #pragma or __attribute__ (as that is not portable either). Instead, do the following:
* Before the struct(s) you need to be packed, insert
* Before the struct(s) you need to be packed, insert
<source lang="cpp">
   #include "common/pack-start.h" // START STRUCT PACKING
   #include "common/pack-start.h" // START STRUCT PACKING
</source>
* After the struct(s) you need to be packed, insert
* After the struct(s) you need to be packed, insert
<source lang="cpp">
   #include "common/pack-end.h" // END STRUCT PACKING
   #include "common/pack-end.h" // END STRUCT PACKING
</source>
* At the "end" of each struct you need to be packed, insert the following between the closing } and the ; after it:
* At the "end" of each struct you need to be packed, insert the following between the closing } and the ; after it:
<source lang="cpp">
   PACKED_STRUCT
   PACKED_STRUCT
 
</source>
You may want to look at some files which already do that, e.g. look at <code>engines/scumm/boxes.cpp</code>
You may want to look at some files which already do that, e.g. look at <code>engines/scumm/boxes.cpp</code>