Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!ut-sally!pyramid!decwrl!sun!guy From: guy@sun.uucp (Guy Harris) Newsgroups: net.lang.c Subject: Re: structure alignment question Message-ID: <7363@sun.uucp> Date: Wed, 17-Sep-86 14:34:20 EDT Article-I.D.: sun.7363 Posted: Wed Sep 17 14:34:20 1986 Date-Received: Fri, 19-Sep-86 23:25:33 EDT References: <101@hcx1.UUCP> Organization: Sun Microsystems, Inc. Lines: 71 > Background: Our architecture has the following data types and sizes: > (fairly "standard" 32-bit architecture description, which could very well > be CCI's Power 6; Harris (no relation) sells this machine under their own > name) > > Question: Should structures and unions always be aligned to 4-byte > boundaries? > Consider: Our portable C compiler aligns this structure as follows: > ... > short a; -> byte 0 > union aa > { > short bb; -> byte 4 > char cc; > } dd ; > char c; -> byte 8 > > I've been told that on a VAX, however, 'bb' is placed on byte 2 and > 'c' is placed on byte 4. Is this true? Yes, at least in the 4.3BSD version of the compiler, although the other versions are almost certainly not different enough to align the values differently. > How do other compilers handle this case? Sun's 68000-family compiler does exactly the same thing, although (obviously) "struct a"s can't be blindly copied between VAXes and Suns. > Which way is "correct"? K&R says: 8.5 Structure and union declarations ...Each non-field member of a structure begins on an addressing boundary appropriate to its type... K&R does not seem to say anything about the addressing boundaries appropriate to structures or unions in the Reference Manual, but it implies that the addressing boundary appropriate to a union, at least, is that of the member with the most restrictive addressing boundary on page 174 of chapter 8, where it suggests that in order to align a given structure on a boundary more restrictive than its "natural" boundary, you place it in a union with an object with the desired alignment boundary. The ANSI C draft seems to say much the same thing, and gives little additional detail. (If it's stored somewhere not indexed under "alignment", let me know; I only looked in the Index under "alignment".) The 4.3BSD VAX C compiler (and, I suspect, all other UNIX VAX C compilers) follow this rule for structures, as well; if you declare a structure containing only one member, a "char", the size of the structure will be one byte and the structure will be aligned only on a byte boundary. In PCC, the minimum alignment requirement of a structure (or union) is given by the #define constant "ALSTRUCT"; this is 8 (bits) in the VAX PCC and in our PCC. I suspect the person who did the compiler for the machine in question may have gotten confused and figured that if the machine requires 4-byte alignment of "int"s, it also requires 4-byte alignment of "struct"s and "union"s (the CCI Power 6 does require 4-byte alignment of "int"s, and 2-byte alignment of "short"s), and set ALSTRUCT to 32 (bits), not realizing that this is a *minimum*, not *maximum* alignment requirement. It may be too late to change it now, given that a lot of code has been compiled for that machine and a lot of on-disk data structures built using that alignment. I'd at least try, though. -- Guy Harris {ihnp4, decvax, seismo, decwrl, ...}!sun!guy guy@sun.com (or guy@sun.arpa)