
C standard allows padding and reorder of struct entries
Almost. The ISO C standard does allow structs padding, but *not* reordering: http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1124.pdf ISO/IEC 9899:1999 C Standard ยง6.7.2.1.13 "Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning." Dan Lutz Donnerhacke wrote:
* Michael D. Adams wrote:
But as far as I can tell, hsc2hs doesn't support bit fields. On top of that I'm not sure I can make any safe assumptions about what order the bit fields are packed (LSB or MSB first).
C standard allows padding and reorder of struct entries in order to match alignment requirements. The only exeption are bitfields, which must not reordered and padded. This way bit fields are the only portable way to define the binary representation in C. Unfortunly the C standard does not specify any bit order for bit fields, but almost all implementations use the machine specific bit order, in order to ease access to multiple bits wide bit field and fill LSB to MSB. But there is no guarantee.