Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 28 Apr 2001 14:40:55 +0200
From:      Erik Trulsson <ertr1013@student.uu.se>
To:        freebsd-stable@freebsd.org
Subject:   Re: g++ bug in FreeBSD-4.3
Message-ID:  <20010428144055.A6153@student.uu.se>
In-Reply-To: <15082.46019.913297.264758@raw.grenland.fast.no>; from Raymond.Wiker@fast.no on Sat, Apr 28, 2001 at 02:12:51PM %2B0200
References:  <15082.46019.913297.264758@raw.grenland.fast.no>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sat, Apr 28, 2001 at 02:12:51PM +0200, Raymond Wiker wrote:
> 	I just had a look at the gcc-bugs mailing list archive, at
> <URL:http://gcc.gnu.org/ml/gcc-bugs/>. The reason for this is that I
> spent a few hours yesterday on some code that worked about a month
> ago, but now misbehaves in odd ways. I suspect that if I had been a
> less experienced programmer, I would have suspected the compiler
> earlier :-)
> 
> 	Anyway, I came across the following bug report:
> <URL:http://gcc.gnu.org/ml/gcc-bugs/2001-04/msg00707.html>. This looks
> pretty serious. The code from that bug report is reproduced below; I
> have verified that on my machine (4.3-STABLE FreeBSD 4.3-STABLE #0:
> Fri Apr 27 09:58:39 CEST 2001), the bug occurs with -O0, -O and -O2.
> 
> #include <iostream>
> using namespace std;
> double f(double& x)
> {
>   x=1;
>   return 2;
> }
> int main()
> {
>    double d=5;
>    cout << f(d) << "\t" << d << endl;
>    // the line below produces correct output of "2    1"
>    // double y=f(d); cout << y << "\t" << d << endl;
> }
> 
> 	This one is *bad* :-(
> 
> 	I don't know how this should be handled; one option might be
> to have a port for gcc 2.95.2.
> 
> 	//Raymond.

I am fairly certain that the bug is in the program, not the compiler.
I am not an expert on C++ but in C at least the order of evaluation of
the different parts of an expression is, in most cases, not specified.
What happens in the program above is almost certainly that 'd' is
evaluated first and then 'f(d)' is evaluated.
Thus the value that is printed for 'd' is the value it had *before* f(d)
was called.
Note that if you change output line to the following it prints "2   1"

cout << f(d) << "t" ; cout << d << endl;

In this case f(d) is guaranteed to be called before d is evaluated.





-- 
<Insert your favourite quote here.>
Erik Trulsson
ertr1013@student.uu.se


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-stable" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010428144055.A6153>