Re: Bug in GCC?

Kevin Lentin (kevinl@cs.monash.edu.au)
Sun, 30 Jul 1995 16:05:57 +1000 (EST)


Douglas Warren Wrote ...
> test.c:
> main() {
> int i;
> double test,ANGLE;
> srandom(getpid());
> for(i=0;i<100;i++) {
> test=.5+double_rand();
> printf("%d %f\n",i,test);
> }
> }
> test1.c:
> double double_rand(void)
> {
> return (double) random() / 2147483648.0;
> }
> Floating point exception
> HOWEVER, if the 2 files are removed, and the double_rand() function
> is in the same source function as it is called, it works flawlessly.

When you combine them, do you put the function before or after main?
If you're putting it before then the problem is obvious. Try move it after
main and the problem should return.

Then try put:
double double_rand();

Before main in the 2 file version.

What is happening is that GCC is assuming (as it should) that the result of
double_rand() is an int. double_rand is returning a double, which GCC then
interprets as an int and then converts it to a double again to add to .5
That's asking for trouble. You must prototype non-int function.s

I just tried it and GCC 2.5.8 agrees with me EXCEPT when I put
double_rand after main as in the following:
main() {
int i;
double test,ANGLE;
srandom(getpid());
for(i=0;i<100;i++) {
test=.5+double_rand();
printf("%d %f\n",i,test);
}
}
double double_rand(void)
{
return (double) random() / 2147483648.0;
}

In this case, GCC tells you what is happening:
test2.c:11: warning: type mismatch with previous external decl
test2.c:6: warning: previous external decl of `double_rand'
test2.c:11: warning: type mismatch with previous implicit declaration
test2.c:6: warning: previous implicit declaration of `double_rand'
test2.c:11: warning: `double_rand' was previously implicitly declared to return
`int'

Any function not prototyped defaults to return int as in the last line of
warning.

-- 
[==================================================================]
[ Kevin Lentin                   |___/~\__/~\___/~~~~\__/~\__/~\_| ]
[ kevinl@cs.monash.edu.au        |___/~\/~\_____/~\______/~\/~\__| ]
[ Macintrash: 'Just say NO!'     |___/~\__/~\___/~~~~\____/~~\___| ]
[==================================================================]