Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!mcsun!ukc!edcastle!dcl-cs!gdt!gdr!exspes From: exspes@gdr.bath.ac.uk (P E Smee) Newsgroups: comp.lang.c,comp.sys.super Subject: Re: Type punning in C Message-ID: <1989Oct16.092936.142@gdt.bath.ac.uk> Date: 16 Oct 89 09:29:36 GMT References: <475@idacrd.UUCP> <1989Oct10.185851.6490@agate.berkeley.edu> <1989Oct11.091619.18336@gdt.bath.ac.uk> <126279@sun.Eng.Sun.COM> Reply-To: exspes@gdr.bath.ac.uk (P E Smee) Organization: University of Bristol c/o University of Bath Lines: 45 In article <126279@sun.Eng.Sun.COM> khb@sun.UUCP (Keith Bierman - SPD Advanced Languages) writes: >In article <1989Oct11.091619.18336@gdt.bath.ac.uk> exspes@gdr.bath.ac.uk (P E Smee) writes: > > >>Problem is, the Fortran standard *also* says that if your program tries >>to take the value of the variable using a different type than the type you >>used when you last stored into it, your program is invalid. > >My quick peek into the document (pages 8-3 and 17-1..4ish) doesn't >make this obvious to me. Could you please quote chapter and verse ? > Section 17.3(2) -- (or, page 17.4, lines 15-18) 'When an entity of a given type becomes defined, all totally associated entities of different type become undefined.' With 17.2(1) 'Execution of an arithmetic, logical, or character assignment statement causes the entity that precedes the equals to become defined. These must, of course, be taken in conjunction with the definition of what it means for something to be defined, and with the fact that if your program uses variables while they are undefined, your program is invalid. From 17.1 it is clear that EQUIVALENCE may cause total association. May also cause only partial association depending on what the equivalanced entities are. In particular, from 17.1.1 single (non-array) integer, real, and logical values equivalenced in any mix are totally associated. >> >>Fortran equivalence was designed toallow reuse of storage on the early >>small memory machines -- not to allow type punning. Usually you can >>get away with punning, but it doesn't always work and so is a bad habit. >> > >On which systems (if any) have you seen this not work ? > Honeywell Multics at certain optimisation levels. Of course, since HIS don't make them any more, that's a moot point. I believe I recall similar occasional problems under various flavours of IBM OSes, again at particular optimisation levels. Doesn't alter the fact that since the standard makes it clear that it can be invalid, it's a bad habit. -- Paul Smee | JANET: Smee@uk.ac.bristol Computer Centre | BITNET: Smee%uk.ac.bristol@ukacrl.bitnet University of Bristol | Internet: Smee%uk.ac.bristol@nsfnet-relay.ac.uk (Phone: +44 272 303132) | UUCP: ...!mcvax!ukc!gdr.bath.ac.uk!exspes