3 ms·
I support a C codebase that, over the years, has been supported on 4 different processor architectures (Alpha, SPARC, x86, Itanium). It's rather simple to main
by Kon-Peki 3y ago
I support a C codebase that, over the years, has been supported on 4 different processor architectures (Alpha, SPARC, x86, Itanium).
It's rather simple to maintain structures with the exact same size and alignment on all of the platforms. Use dummy variables, group all structure members by type, and put character fields last.
Put a comment to the right of every member that contains the expected byte offset of that member. At the end of the structure, #define the total expected structure size in bytes, because as part of your test suite you have test cases that assert structure sizes (and member alignments). It's a little extra work at the start, but once you have that habit it's really simple and quick.
typedef struct example {
int mbr1; // 0
int dummy; // 4 - dummy variable, unused
double mbr2; // 8
double mbr3; // 16
} example;
#define S_EXAMPLE_BYTES 24