[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
On Sun, 06 Jan 2008, David Laight wrote:
> > memcpy(inqbuf->vendor, "ADAPTEC ACB-4000 ", 28);
> > yet inqbuf->vendor is declared in struct scsipi_inquiry_data as:
> > char vendor;
> and is followed by:
> char product;
> char revision;
> so the memcpy updates all 3 fields :-)
This is rather ugly, but it's either guaranteed to work, or will work
with all except the most perverse C language implementations.
The C99 standard (I have the WG14/N1124 draft) says, in section 18.104.22.168
paragraph 5, that storage for members of a structure are allocated in an
ordered sequence; and paragraph 13 says that the members have addresses
that increase in the order in which they are declared.
Section 22.214.171.124 paragraph 12 says that structure members are aligned in
an implementation-defined manner appropriate to their type.
Section 126.96.36.199 paragraph 13 says that there may be unnamed padding
within a structure.
Now, "aligned ... appropriate to its type" means that a char array is
aligned to char boundaries. This implies that two adjacent char array
members in a struct will have adjacent addresses. I can't find an
explicit prohibition against gratuitous padding that's not actually
required to satisfy aligment constraints, but section 7.21.4 footnote
265 implies that padding in structures is "for purposes of alignment".
So, even if this is not guaranteed to work, it would take a very
perverse implementation to add unwanted padding between char arrays in
--apb (Alan Barrett)
Main Index |
Thread Index |