home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!europa.asd.contel.com!howland.reston.ans.net!usc!cs.utexas.edu!torn!watserv2.uwaterloo.ca!watmath!thinkage!atbowler
- From: atbowler@thinkage.on.ca (Alan Bowler)
- Subject: Re: Is this ANSI?
- Message-ID: <1992Dec15.204100.4293@thinkage.on.ca>
- Organization: /etc/organization
- References: <By6pC6.3u7@unx.sas.com> <1992Dec1.225840.719@thinkage.on.ca> <Byr66A.A2A@unx.sas.com>
- Date: Tue, 15 Dec 1992 20:41:00 GMT
- Lines: 53
-
- In article <Byr66A.A2A@unx.sas.com> sasghm@theseus.unx.sas.com (Gary Merrill) writes:
- >
- >In article <1992Dec1.225840.719@thinkage.on.ca>, atbowler@thinkage.on.ca (Alan Bowler) writes:
- >|> >
- >|> >Nonsense. See section 3.5.2.1: "Within a structure object, the
- >|> >non-bit-field members and the units in which bit-fields reside have
- >|> >addresses that increase in the order in which they are declared."
- >|>
- >|> True. The compiler is not free to juggle the order withing a
- >|> struct, but as a rule of thumb you you program as if it did have that
- >|> freedom. Similarly, you should treat "union" as if the compiler was
- >|> free pretend it read "struct".
- >
- >Huh? Why should you do this? What do you gain by doing so? Would
- >you care to justify these "rules of thumb"? (There are some pretty
- >silly consequences to supposing that a compiler might really *not*
- >enforce the standard in this respect. But right now I leave these
- >as an exercise for the reader. As it is, the suggestion that one
- >ought to suppose that the compiler *fails* to implement an explicit
- >part of the standard is puzzling enough.)
- >
- Because at a later time you may want to reorder the structure so that
- it gives you better performance on a different machine. If you have
- done things that depend on the order of objects within the struct,
- you will find it difficult to locate these dependancies.
-
- >By the way, do you also (as a rule of thumb) program as if the
- >compiler might (for example) implement a long as a smaller type
- >than a short?
- >
- >And finally ... If you really *do* treat 'union' as though it might
- >mean 'struct' (although I confess I am not at all sure what it *means*
- >to treat 'union' in this way), you could be in *real* trouble since
- >sequences of operations guaranteed to succeed for structs will fail
- >for unions.
- What I meant is that except for machine dependant code, the program
- should only retrieve a value from a union element, if the last
- assignment to that union was to the SAME element name. I know that
- there are things that are guaranteed to work on structs that will fail
- for unions. I am not refering to these cases. There are also some
- things that "work" if you have a union, but would fail if you had
- a struct. These should be avoided.
- E.g.
- union {
- int i;
- char c;
- } u;
-
- u.i = 'a';
- printf("%c", u.c);
- will "work" on some machines. This is the sort of thing that should
- be avoided. The last assignment to u was to u.i so only u.i or all
- of u should be accessed as a value.
-