Date: Tue, 4 Aug 1998 17:43:34 -0400 (EDT) From: Chuck Robey <chuckr@glue.umd.edu> To: Thomas David Rivers <rivers@dignus.com> Cc: Nicolas.Souchu@prism.uvsq.fr, freebsd-hackers@FreeBSD.ORG Subject: Re: C and static initialization with unions Message-ID: <Pine.BSF.4.00.9808041738180.409-100000@picnic.mat.net> In-Reply-To: <199808042227.SAA16749@lakes.dignus.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, 4 Aug 1998, Thomas David Rivers wrote: > Also - you should be aware that ANSI forbids assigning to one element > of a union and referencing a different element. (Basically, the > 'type-punning' problem) C compilers are allowed to optimize field > references based on type; so that: > > main() > { > union foo_t foo; > void *vp; > > foo.i = 5; > foo.p = 0; > > if(foo.i) { > printf("true\n"); > } else { > printf("false\n"); > } > } > > may, depending on the optimizer, print either true or false. Technically, > this is an invalid ANSI C program. I never saw that, and I think you may be wrong, so if you have the ansi standard lying around, give me a quote, ok? Harbison & Steele says, on page 132-133, that doing what you said above is non-portable (I agree), but programmers "sometimes do this to 'reach under' C's type system ...". There was more, but I'm sure you know it already. ----------------------------+----------------------------------------------- Chuck Robey | Interests include any kind of voice or data chuckr@glue.umd.edu | communications topic, C programming, and Unix. 213 Lakeside Drive Apt T-1 | Greenbelt, MD 20770 | I run Journey2 and picnic (FreeBSD-current) (301) 220-2114 | and jaunt (NetBSD). ----------------------------+----------------------------------------------- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.00.9808041738180.409-100000>