Original Post
Hey all, I'm creating some data files for my game, and one of the problems I have encountered is structure byte-alignment. For example, the map-tile structure I use is 6 bytes in size (an unsigned integer and a signed short integer), however when I write this file to disk, e.g. ... the size of a data file written to disk indicates a byte-alignment of 8, rather than 6. Is there an elegant method of handling this, given that I am designing my game engine cross-platform (specifically Win32, Linux, and eventually Mac OS X, all on 32 bit CPU platforms)? Currently I am writing out individual structure members (e.g. one fwrite() each for 'type' and 'height' in the example above), however if I follow this method throughout my game, anytime a struct's/class's structure is revised, it means rewriting a lot of code. I'm aware of #pragma pack(n) (where n is the byte boundary to use), however is this a trustworthy method of handling this problem? Thanks for any input :)
struct
{
unsigned int type;
short height;
} tile;
tile t;
fwrite(&t, sizeof(tile), 1, outfile);