Related articles |
---|
Permuting fields of records garavel@imag.fr (1993-06-04) |
Re: Permuting fields of records drw@runge.mit.edu (1993-06-06) |
Re: Permuting fields of records nickh@CS.CMU.EDU (1993-06-08) |
Re: Permuting fields of records henry@zoo.toronto.edu (1993-06-09) |
Newsgroups: | comp.compilers |
From: | henry@zoo.toronto.edu (Henry Spencer) |
Keywords: | C |
Organization: | U of Toronto Zoology |
References: | 93-06-012 93-06-017 |
Date: | Wed, 9 Jun 1993 15:35:31 GMT |
drw@runge.mit.edu (Dale R. Worley) writes:
>[ANSI C] In addition, it may be that structs need not be
>padded to a multiple of their alignment restriction. (Can "struct { short
>a; char b; }" be allocated only 3 bytes?)
In general, no. The size of a struct isn't context-dependent, which means
it must include enough padding so that an array of struct would put all
members at suitable alignments. (Arrays, unlike structs, are *not*
allowed to contain unnamed padding.) If a short needs 2-byte alignment,
that struct must be padded to 4 bytes. On the other hand, this one:
struct threebyte { char foo[3]; }
doesn't need any padding (although many compilers, in practice, pad all
structs to worst-case alignment, in which case this one would be padded).
On the other other hand, there is always ANSI C's "as if" rule, which
says that the compiler can do anything it pleases if well-behaved
programs can't tell the difference. (Roughly speaking.)
--
Henry Spencer @ U of Toronto Zoology, henry@zoo.toronto.edu utzoo!henry
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.