On portable C programming (was: libraw1394 struct layouts...)

Ben Scott dragonhawk at gmail.com
Thu Jan 8 13:50:57 EST 2009


On Thu, Jan 8, 2009 at 7:41 AM, Jerry Feldman <gaf at blu.org> wrote:
> On 01/07/2009 04:10 PM, Ben Scott wrote:
>> The C standard provides no guarantees
>> about how struct's will be laid out in memory.  The compiler can do
>> (and often does) whatever it want.  As we see here, compilers often
>> align/pad struct's to optimize for the architecture they're targeting.
>
> The compiler is required to maintain the order of the elements in
> a struct.

  Ah, I didn't know that.  (Or if I did, I had forgotten.)  I stand corrected.

> Many times it is the calling standard that mandates alignment, not the
> compiler.

  Right, but sometimes when the compiler changes, how it implements
those mandates changes as well.  Back on the early days of GCC on the
Alpha, GCC was still evolving to do a better job.  I remember
something along the lines of a compiler update that resulted in data
structures compiled with version Y not being compatible with data
structures from version X, from the same source code.  Granted, the
code in question was probably not written with portability in mind,
but the programmer probably didn't think portability was important
because the code was "never" going to run on another machine.  He
didn't foresee a compiler update introducing portability concerns.

> Additionally, pragmas are not standard and are implementation defined.

  Absolutely.  I once read that an early version of GCC didn't
implement any pragma's, so the "implementation defined behavior" on
encountering a pragma was to attempt to start Emacs and run the
"psychoanalyze-pinhead" command.

  "When the compiler encounters [a given undefined construct] it is
legal for it to make demons fly out of your nose."  -- John Woods

-- Ben


More information about the gnhlug-discuss mailing list