Re: structure layout question
- From: roberson@xxxxxxxxxxxxxxxxxx (Walter Roberson)
- Date: Thu, 1 Mar 2007 19:17:19 +0000 (UTC)
In article <87hct4240f.fsf@xxxxxxxxxxxxxxxx>,
Ben Pfaff <blp@xxxxxxxxxxxxxxx> wrote:
roberson@xxxxxxxxxxxxxxxxxx (Walter Roberson) writes:
[in a struct]
As best I recall, arbitrary padding is not permitted: only
where required to bring the entry into alignment (where alignment
rules are platform dependant.) So if you know the alignment rules
for (say) a "pure" double, then you also know the alignment
rules for a double embedded in a struct.
This is a nice theory, but I can't see how to back it up with a
quote from the standard. The text of the standard says "There
may be unnamed padding within a structure object, but not at its
beginning." and I don't see any restrictions on that.
There is a bit more wording that that in C89 3.5.2.1:
Each non-bit-field member of a structure or union object is
aligned in an implementation- defined manner appropriate to its type.
Within a structure object, the non-bit-field members and the units
in which bit-fields reside have addresses that increase in the order
in which they are declared. A pointer to a structure object,
suitably converted, points to its initial member (or if that
member is a bit-field, then to the unit in which it resides), and
vice versa. There may theefore be unnamed padding within a
structure object, but not at its beginning, as necessary to achieve the
appropriate alignment.
Notice that the alignment within the structure is in a manner
"appropriate to its type". I interpret that as the alignment
appropriate to the type "ex-vivo", outside of structure. I do not
see anything there that would suggest that a member could have one
alignment within structures and a different alignment outside of
structures.
Notice the padding is not "for arbitrary purposes", but only
"as necessary to achieve the appropriate alignment".
I only apply this hypothesis to the padding -inside- the structure,
and not to any "trailing" padding of the structure. For example,
the classic struct {int i; char c;} might have trailing padding
so that you can form effecient arrays of such elements.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
.
- Follow-Ups:
- Re: structure layout question
- From: Eric Sosman
- Re: structure layout question
- From: Ben Pfaff
- Re: structure layout question
- References:
- structure layout question
- From: kyle york
- Re: structure layout question
- From: Walter Roberson
- Re: structure layout question
- From: Ben Pfaff
- structure layout question
- Prev by Date: Re: structure layout question
- Next by Date: Re: Passing execution to a memory address
- Previous by thread: Re: structure layout question
- Next by thread: Re: structure layout question
- Index(es):
Relevant Pages
|