Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Kaz Kylheku <kaz@kylheku.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: struct alignment |
| Date | 2012-01-18 17:21 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <20120118091128.539@kylheku.com> (permalink) |
| References | (1 earlier) <lnipkh3lpg.fsf@nuthaus.mib.org> <87hb01v279.fsf@temporary-address.org.uk> <9n7h6lFvfbU5@mid.individual.net> <ln62gh2pes.fsf@nuthaus.mib.org> <slrnjhdurr.1ls.grahn+nntp@frailea.sa.invalid> |
On 2012-01-18, Jorgen Grahn <grahn+nntp@snipabacken.se> wrote:
> On Thu, 2012-01-12, Keith Thompson wrote:
>> Ian Collins <ian-news@hotmail.com> writes:
>>> On 01/12/12 07:56 PM, Dr Nick wrote:
>>>> Keith Thompson<kst-u@mib.org> 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.
C has an offsetof macro, such that offsetof(struct Foo, a) == 0 has to hold,
and the offsets of the other members have to be in increasing order.
Reordering struct members would break all kinds of code, not all of which is
ISO C mandated.
For instance maintaining backward binary compatibility in shared library
interfaces for structures that are allocated outside of the library
(e.g. in static or automatic storage in client code).
/* Maintainers! Add new members only at the end. */
struct Foo {
int a; /* Lib 1.0 members */
char b;
int c; /* Added in 1.2 */
char d; /* New in 1.3 */
};
In this case, the above struct has to be compatible with the one
compiled in the existing 1.0 clients which is just this:
struct Foo {
int a; /* Lib 1.0 members */
char b;
};
(Knowledge about which type of client is attached to the library can be used to
carefully avoid touching members c and d.)
Sometimes structs are used to conform to an external storage layout,
or hardware registers.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
struct alignment "Mark" <mark_cruzNOTFORSPAM@hotmail.com> - 2012-01-11 13:31 -0500
Re: struct alignment Kleuske <kleuske@nowhere.net> - 2012-01-11 19:23 +0000
Re: struct alignment "Scott Fluhrer" <sfluhrer@ix.netcom.com> - 2012-01-11 16:58 -0500
Re: struct alignment Ralf Damaschke <rwspam@gmx.de> - 2012-01-11 22:08 +0000
Re: struct alignment Keith Thompson <kst-u@mib.org> - 2012-01-11 14:42 -0800
Re: struct alignment Dr Nick <3-nospam@temporary-address.org.uk> - 2012-01-12 06:56 +0000
Re: struct alignment Ian Collins <ian-news@hotmail.com> - 2012-01-12 20:42 +1300
Re: struct alignment Keith Thompson <kst-u@mib.org> - 2012-01-12 02:20 -0800
Re: struct alignment Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-18 17:03 +0000
Re: struct alignment Kaz Kylheku <kaz@kylheku.com> - 2012-01-18 17:21 +0000
Re: struct alignment jgk@panix.com (Joe keane) - 2012-01-19 21:42 +0000
Re: struct alignment blp@cs.stanford.edu (Ben Pfaff) - 2012-01-19 13:57 -0800
Re: struct alignment Stephen Sprunk <stephen@sprunk.org> - 2012-01-18 11:57 -0600
Re: struct alignment Keith Thompson <kst-u@mib.org> - 2012-01-18 14:28 -0800
Re: struct alignment Robert Wessel <robertwessel2@yahoo.com> - 2012-01-18 13:10 -0600
Re: struct alignment Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-19 11:34 +0000
Re: struct alignment Robert Wessel <robertwessel2@yahoo.com> - 2012-01-19 14:50 -0600
Re: struct alignment Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-21 11:49 +0000
csiph-web