On Wed, 2012-01-18, Robert Wessel wrote:
> On 18 Jan 2012 17:03:55 GMT, Jorgen Grahn <grahn+>
> wrote:
>
>>On Thu, 2012-01-12, Keith Thompson wrote:
>>> Ian Collins <ian-> writes:
>>>> On 01/12/12 07:56 PM, Dr Nick wrote:
>>>>> Keith Thompson<kst-> writes:
>>>>>
>>>>>> The compiler will (in fact, it *must*) insert whatever padding is
>>>>>> necessary so that all the members can be accesssed correctly. It may or
>>>>>> may not insert more padding than it strictly needs to.
>>>>>>
>>>>>> The language standard guarantees that the first declared member is at
>>>>>> offset 0, and that all other members are at increasing offsets in the
>>>>>> order in which they're declared (the compiler can't rearrange
>>>>>> members).
>>>>>
>>>>> Does any compiler let you do this? I have done things where the order
>>>>> of a least some elements (usually the first or the last) matters, but
>>>>> it's rare.
>>>>
>>>> Let you do what? I didn't see anything optional in Keith's reply.
>>>
>>> Permit reordering of struct members, I presume. Plenty of C
>>> compilers have plenty of options that break standard compliance.
>>
>>But is it really breaking compliance?
>>
>> struct Foo {
>> int a;
>> char b;
>> int c;
>> char d;
>> };
>>
>>If the compiler lays these out as a-c-b-d, is that really something
>>which is observable using correct C code?
>>
>>I seem to recall (but I can be wrong) that the expression
>>&foo.b < &foo.d is not guaranteed to be meaningful, unlike e.g.
>>comparisons inside an array.
> It's also required that structures with common leading sections have
> the same in-storage layout.
Ok, that is enough reason for not allowing reordering. To me, the
real reason then seems to be to allow things like the POSIX struct
sockaddr/sockaddr_in/sockaddr_in6/... hack. I always assumed the
sockaddr stuff demanded stronger guarantees from the compiler than the
standard gives.
> Of course that's all subject to the as-if rule, if the compiler can
> determine that the program never looks at the ordering, or the
> compiler can fake it, it's allowed to do anything it wants under the
> covers, including omitting and reordering parts or all of the
> structure.
Yes, but that surely is rare.

Most structs are not limited to one
translation unit.
I agree with whoever-it-was upthread: if it was allowed to do it, the
compiler could do a much better layout of my structs than I am willing
to do manually.
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .