Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!linus!philabs!micomvax!musocs!mcgill-vision!mouse From: mouse@mcgill-vision.UUCP Newsgroups: comp.lang.c Subject: Re: Bug converting unsigned to double in BSD 4.[23] Message-ID: <562@mcgill-vision.UUCP> Date: Sat, 22-Nov-86 19:45:17 EST Article-I.D.: mcgill-v.562 Posted: Sat Nov 22 19:45:17 1986 Date-Received: Mon, 24-Nov-86 19:29:54 EST References: <213@cartan.Berkeley.EDU> <618@hadron.UUCP> <295@hao.UUCP> Organization: McGill University, Montreal Lines: 45 In article <295@hao.UUCP>, woods@hao.UUCP (Greg Woods) writes: > On a VAX, and every machine I've worked on EXCEPT the ISI-68K, the > first 32 bits of a double form a float. And it has led to some of the *sloppiest* code, just because some machines will let you get away with it.... > Not true on the 68000. Consider the following trivial C program: > main() { float f=1.0; test1(f); exit(0); } > test1(f) float f; { printf("test1: f=%f\n",f); test2(&f); } > test2(f) float *f; { printf("test2: f=%f\n",*f); > The reason is that according to the C standard, when f is passed to > test1, it is converted to a double and placed on the stack. [...] so > that test2 is passed the address of something that is really a > double, not a float as declared in the code. This is a BUG, [...] Yes - in your code. K&R, page 205: C converts all float actual parameters to double, so formal parameters declared float have their declaration adjusted to read double. That is, the argument to test1 is not a float, it is a double (which has been declared in a misleading manner). Thus test1 is passing a pointer to double, not to float. But test2 is expecting a pointer to float, not to double. Of *course* it's losing! > it DOESN'T CONVERT the stacked value back to a float before calling > test2, so that test2 is passed the address of something that is > really a double, not a float as declared in the code. The code declares f to be a double, albeit in a confusing way. I agree that having C behave this way is a misfeature. I posted something very similar to net.lang.c a while ago, and someone (I forget who) pointed this detail of the C definition out to me then. der Mouse USA: {ihnp4,decvax,akgua,utzoo,etc}!utcsri!mcgill-vision!mouse think!mosart!mcgill-vision!mouse Europe: mcvax!decvax!utcsri!mcgill-vision!mouse ARPAnet: think!mosart!mcgill-vision!mouse@harvard.harvard.edu [USA NSA food: terrorist, cryptography, DES, drugs, CIA, secret, decode]