Possible C compiler bug?
Steve Lamont
spl at cs.nps.navy.mil
Mon Sep 10 15:14:33 AEST 1990
I'm having a problem with a piece of C code on a 4D/70GT under 3.2. I have
declared a variable as float (actually Coord but they're effectively the
same). I pass it as a parameter to a function foo() and foo() passes it as a
pointer to function bar(). When bar gets the float and dereferences the
pointer, it finds a bogus value. It seems what C is doing is promoting the
variable to a double even though it is declared as a float.
Some investigation reveals that floats and doubles are constructed
differently, with 8 bits for exponent and (I think) sign for floats and 12
bits for doubles. Clearly, referencing a double aliased as a float is a bad
thing.
The following test code shows the problem:
---------------------------------8<-----------------------------------------
#include <stdio.h>
main()
{
float x = 123.345;
foo( &x, x );
exit( 0 );
}
foo( a, b )
float *a;
float b;
{
/*
* *a and b should be the same.
*/
fprintf( stderr, "foo: %f %f\n", *a, b );
/*
* Now call with b as a pointer and as a value.
*/
bar( &b, b );
return;
}
bar( a, b )
float *a;
float b;
{
/*
* *a and b should be the same. On our 4D they ain't.
*/
fprintf( stderr, "bar: %f %f\n", *a, b );
return;
}
------------------------------------8<---------------------------------------
Here's what the SillyG returns:
foo: 123.345001 123.345001
bar: 3.481816 123.345001
and here's what a BSD VAX 11/785 sez:
foo: 123.345001 123.345001
bar: 123.345001 123.345001
I've looked through K&R (2nd Edition) and can't find anything that
specifically bears upon this problem in the sections on type coersion. Am I
doing something wrong?
spl (the p stands for
perplexed over
precision)
--
Steve Lamont, SciViGuy -- (408) 646-2752 (subject to change at random)
NPS Confuser Center / Code 51 / Naval Postgraduate School / Monterey, CA 93940
"You're okay," said Honeysuckle. "The dogs like you."
- Charles Bukowski, "How to Get Published"
More information about the Comp.sys.sgi
mailing list