Article View: comp.sys.atari.st
Article #26357Re: Type coercion
From: Matt Dillon
Date: Mon, 13 Apr 1987 03:34
Date: Mon, 13 Apr 1987 03:34
37 lines
1753 bytes
1753 bytes
>I haven't used unions much, but isn't it true that C may attempt to
>re-align the items within a structure (or union) to lie on proper
>boundaries (e.g. word boundaries, longword boundaries, etc) where
>necessary? This means that there may be padding bytes within the
>structure. Could this cause errors where the elements of the structures
>within the union don't lie "on top of one another" as one would
>expect?
C will always align entries in a structure or union properly for
that machine, but you should never count on it. If alignment is absolutely
required for your application, you can assume that C will not re-align
anything already on sizef(char *) (usually longword) boundries. chars are
not usually re-aligned. In anycase, this is portable over most machines.
In terms of having two data items conflicting in a union, here is
an example (this is not neccessarily how your compiler may do things):
struct {
char c;
union {
char d;
char *x;
} u;
};
&c = +0
&u.d = +4
&u.x = +4
[c][unused][unused][unused][d][][][]
[x..4byt]
You should *never* rely on unions aligning things in some known manner
if the data items inside the union are different sizes.
-Matt
Technical Details
Message-ID:
<8704130334.AA19440@cory.Berkeley.EDU>Path:
arc1-ca.pugleaf.net!utzoo!mnetor!seismo!rutgers!mit-eddie!genrad!decvax!ucbvax!CORY.BERKELEY.EDU!dillonShow Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!rutgers!mit-eddie!genrad!decvax!ucbvax!CORY.BERKELEY.EDU!dillon From: dillon@CORY.BERKELEY.EDU (Matt Dillon) Newsgroups: comp.sys.atari.st Subject: Re: Type coercion Message-ID: <8704130334.AA19440@cory.Berkeley.EDU> Date: Sun, 12-Apr-87 22:34:30 EST Article-I.D.: cory.8704130334.AA19440 Posted: Sun Apr 12 22:34:30 1987 Date-Received: Sat, 18-Apr-87 17:40:45 EST Sender: daemon@ucbvax.BERKELEY.EDU Lines: 37