Topic: Increment Problem
Author: paulp.removethis@ccnet.com (Paul)
Date: 1999/04/14 Raw View
I believe that section 5.0.4 of the standard applies here:
"Except where noted, the order of evaluation of operands
of individual operators and subexpressions of individual
expressions, and the order in which side effects takes
place, is unspecified." ...
Paul
------------------------------------------------------------------
>void main(){
> int a = 0, b = 0;
> if (++a == a) { cout << "true"; }
> else {cout << "false";}
> if (b++ == b) { cout << "true"; }
> else {cout << "false";}
>}
>both return true. but why? can somebody explain it thoroughly, thx.
------------------------------------------------------------------
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Mick" <lovejune@hongkong.com>
Date: 1999/04/11 Raw View
Here is a c++ problem:
void main()
{
int a = 0, b = 0;
if (++a == a) { cout << "true"; }
else {cout << "false";}
if (b++ == b) { cout << "true"; }
else {cout << "false";}
}
both return true. but why? can somebody explain it thoroughly, thx.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/04/13 Raw View
"Mick" <lovejune@hongkong.com> writes:
>Here is a c++ problem:
>void main()
>{
> int a = 0, b = 0;
> if (++a == a) { cout << "true"; }
> else {cout << "false";}
> if (b++ == b) { cout << "true"; }
> else {cout << "false";}
>}
>both return true. but why? can somebody explain it thoroughly, thx.
The code has unspecified results. You increment and test a value
without an intervening sequence point. The value on the right-hand
side of the comparison could be fetched either before or after
the object in memory is incremented. In more complex expressions
you could get varying results with the same compiler. With
different compilers you could get different results.
Most good C++ textbooks discuss sequence points. If you don't find
a good explanation in your favorite one, you can find a description
in the standard, section 1.9, "Program execution".
BTW, "void main" is not allowed in C++. If your compiler accepts
it, it is providing a non-standard extension.
--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "J.Barfurth" <techview@bfk-net.de>
Date: 1999/04/13 Raw View
Mick schrieb in Nachricht <7eqagg$p6d$1@hfc.pacific.net.hk>...
>Here is a c++ problem:
>
>void main()
Use: int main(), it's required
>{
> int a =3D 0, b =3D 0;
>
> if (++a =3D=3D a) { cout << "true"; }
> else {cout << "false";}
>
> if (b++ =3D=3D b) { cout << "true"; }
> else {cout << "false";}
>}
>
>both return true. but why?
By chance. The order of evaluation of subexpressions is unspecified. AFAI=
K
your compiler might do it just the opposite way on your next build.
Certainly other compilers, compiler versions or compilation options (e.g.
optimization) might yield different results. Any combination of
(true,true),( true,false),( false,true),(false,false) is possible.
Please excuse me, if my wording isn't fully correct. I don't have the
standard available at my current location, so I also can't point you to t=
he
relevant paragraph.
-- J=F6rg Barfurth
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]