A couple of years back, I ran into a nasty problem with structure packing that made me get acquainted with structure padding and packing.<p>The company I was working at used the OpenWatcom compiler that lets you tune structure padding with a compiler flag. Also, they "serialized" their data by simply writing arrays of structs to files and reading them back in later. Raw binary files, no conversion whatsoever.<p>So one day - I hadn't been there very long - I was given the task to make a certain change to the application, as well as a copy of a "database" to experiment on. I made a first adjustment, compiled my code, ran it and - BAM! It took me nearly a day to figure out that I had "forgotten" to pass a certain variable to make which in turn resulted in a certain parameter being passed to the Watcom compiler that caused it to use no structure padding at all.<p>Without that variable/parameter, I had compiled the program to use a different padding (i.e. use padding at all), so when my program read the data file (having been dumped by a version of the program being compiled without padding), it barfed, so to speak.<p>That problem caused me to do a little reading on the matter. I sure wish somebody had pointed that article out to me back then. (Assuming it had existed back then, with "back then" being about October 2007.)
This is a good article that comes up every so often. If you haven't read it, you should. The art is not quite as lost as advertised, though. A lot of C programmers pack structures that way out of habit.<p>There's definitely some cases where you shouldn't follow the "biggest to smallest" ordering, but they're usually pretty obvious when you run into them. They usually involve inheritance and such.
In the example in part 4, doesn't this:<p><pre><code> char *p; /* 8 bytes */
long x; /* 8 bytes */
char c; /* 1 byte */
</code></pre>
Just end up being this, to align whatever comes after it in memory?<p><pre><code> char *p; /* 8 bytes */
long x; /* 8 bytes */
char c; /* 1 byte */
char pad[7]; /* 7 bytes */</code></pre>