Re: Permuting fields of records (Henry Spencer)
Wed, 9 Jun 1993 15:35:31 GMT

          From comp.compilers

Related articles
Permuting fields of records (1993-06-04)
Re: Permuting fields of records (1993-06-06)
Re: Permuting fields of records nickh@CS.CMU.EDU (1993-06-08)
Re: Permuting fields of records (1993-06-09)
| List of all articles for this month |

Newsgroups: comp.compilers
From: (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 (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, utzoo!henry

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.