Article View: comp.std.c
Article #4885Re: alignment requirements for all structures?
From: Mark Brader
Date: Tue, 12 Mar 1996 17:59
Date: Tue, 12 Mar 1996 17:59
63 lines
3639 bytes
3639 bytes
Wil Evers (wil@ittpub.nl) writes:
> (1). Is there a rule in the C standard stating that the address of *every*
> structure - irrespective of its contents - should satisfy some alignment
> requirement? ...
No, there is no such rule. The example code invokes undefined behavior.
Each type has an alignment requirement, but each structure type is a
different type with no necessary relation to any other structure type.
> (2). If the answer to (1) is no, then is a conforming compiler allowed to
> assign some specific alignment requirement to a data type, just because it
> is defined as a structure, and again irrespective of the contents of the
> structure?
Yes, it can. Each structure type is a different type with no necessary
relation to any other NON-structure type either.
> (3). If the answer to (2) is yes, why would a compiler want to do this?
On a word-addressed machine, the natural type of pointer -- the one which
occupies one word and probably is the same size as an int -- points to a
whole word. A pointer to an individual byte requires additional storage,
another word or halfword, to identify which byte in the word being pointed
at is meant; and one or more additional operations are also necessary when
dereferencing it, so access via the pointer is slower.
By aligning all structs on word boundaries, the implementation can use
word-pointers rather than byte-pointers whenever pointers to structs are
required.
> For example, if I write:
> #include <stdio.h>
> struct c { char data; } carray[10];
> int main() { printf("%u\n", sizeof carray); return 0; }
... then on a word-addressed machine the output might very well be "40". On
that machine, sizeof (struct c *) would probably be 4 while sizeof (char *)
would be 8, or maybe 6.
Also, some machines are byte-addressed but provide faster access to objects
on word boundaries. Implementers might choose to place everything that they
could on word boundaries whether it was strictly necessary or not. After
all, *you* can always go and buy more memory, right?
It also seems conceivable that an implementer might want to align all
structures *smaller than the machine's register size* on a word boundary
so that struct assignment operations could be implemented efficiently
by loading into, and storing from, a register. I have had personal
experience with a compiler where this was *not* done and the code
I was maintaining, which assigned these little structs a lot, suffered
a measurable speed penalty. (We ended up replacing the original struct
type, on implementations where there was an integral type of the same
size, with a union of the two, using the integral type for assignments
and the struct for member access. [Yes, this is not strictly conforming.
I think it should be; it's safe enough in practice.])
--
Mark Brader | "On a word boundary, Luke, don't just hack at it. ...
msb@sq.com | The bytesaber is the ceremonial weapon of the Red-Eye Knight.
SoftQuad Inc. | It is used to trim offensive lines of code. Handwaving won't
Toronto | get you anywhere. Attune yourself with the Source."
| -- Steve Tarr, Alan Hastings, and Eric Raymond
My text in this article is in the public domain.
Technical Details
Message-ID:
<1996Mar12.175916.7256@sq.com>Path:
arc1-ca.pugleaf.net!howland.reston.ans.net!torn!sq!msbReferences:
<1996Mar7.151216.1793@ittpub>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c Path: arc1-ca.pugleaf.net!howland.reston.ans.net!torn!sq!msb From: msb@sq.com (Mark Brader) Subject: Re: alignment requirements for all structures? Message-ID: <1996Mar12.175916.7256@sq.com> Organization: SoftQuad Inc., Toronto, Canada References: <1996Mar7.151216.1793@ittpub> Date: Tue, 12 Mar 1996 17:59:16 GMT