From owner-freebsd-hackers Tue Aug 4 15:45:36 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id PAA10185 for freebsd-hackers-outgoing; Tue, 4 Aug 1998 15:45:36 -0700 (PDT) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from picnic.mat.net ([209.118.174.117]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id PAA10155 for ; Tue, 4 Aug 1998 15:45:31 -0700 (PDT) (envelope-from chuckr@glue.umd.edu) Received: from localhost (chuckr@localhost) by picnic.mat.net (8.8.8/8.8.5) with SMTP id RAA01869; Tue, 4 Aug 1998 17:43:34 -0400 (EDT) Date: Tue, 4 Aug 1998 17:43:34 -0400 (EDT) From: Chuck Robey X-Sender: chuckr@picnic.mat.net To: Thomas David Rivers cc: Nicolas.Souchu@prism.uvsq.fr, freebsd-hackers@FreeBSD.ORG Subject: Re: C and static initialization with unions In-Reply-To: <199808042227.SAA16749@lakes.dignus.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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