Re: structure layout question



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
.



Relevant Pages

  • Re: structure layout question
    ... arbitrary padding is not permitted: ... where required to bring the entry into alignment (where alignment ... the other member of which is an array of unsigned char. ...
    (comp.lang.c)
  • Re: Data alignment problems with sizeof and new
    ... >I have a strange problem with a project involving classes with data alignment ... It appears that, in this particular project, the sizeof and new ... >operators are not taking the alignment padding into account when calculating ... >class with the first member being a dword size followed by doubles, ...
    (microsoft.public.vc.language)
  • Data alignment problems with sizeof and new
    ... I have a strange problem with a project involving classes with data alignment ... It appears that, in this particular project, the sizeof and new ... operators are not taking the alignment padding into account when calculating ... class with the first member being a dword size followed by doubles, ...
    (microsoft.public.vc.language)
  • Re: [PATCH] block: fix residual byte count handling
    ... Are you suggesting moving them back to drivers? ... with adjusting sglist entries in the block layer with your secode ... patch (separate out padding from alignment). ...
    (Linux-Kernel)
  • Re: [JW] alignment problem for structures
    ... Is that enough to make the alignment requirements apply to ... Array elements include interelement ... >> alignment padding. ... to the start of the struct, then the assignment to *q1 will not "work". ...
    (comp.std.c)