Bug converting unsigned to double in BSD 4.[23]

mouse at mcgill-vision.UUCP mouse at mcgill-vision.UUCP
Sun Nov 23 10:45:17 AEST 1986


In article <295 at hao.UUCP>, woods at 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 at harvard.harvard.edu

[USA NSA food: terrorist, cryptography, DES, drugs, CIA, secret, decode]



More information about the Comp.lang.c mailing list