Re: Same code and different result, Why?
- From: "Alex Blekhman" <tkfx.REMOVE@xxxxxxxxx>
- Date: Sat, 8 Nov 2008 08:57:19 +0200
"Tommy" wrote:
[...] given all the different constructs you can come up that
emulate the same idea, all of them, produce the same result but
the one Microsoft choose for its compiler for the simplest
construct:
x = x++ + y++;
So while we all agreed and acknowledge that this is generally
considered an axiom that may produce different results depending
on the compiler, when you look at the BCP (Best Current
Practice) that exist for the possible results, the MS solution
is the least common one.
I agree with Nathan on this. I can't say with such confidence that
MS compiler stands out in any significant way when we speak about
order of evaluation. I should admit that I have less experience
than you, but cannot recall any consistency regarding undefined
order of evaluation among other non-MS compilers. I was taught
long time ago not to rely on such constructs because they depend
on the mood of optimizer.
Moreover, Stroustrup talks about this very case in the article I
posted:
<quote>
int i = 10;
int j = ++i + i++; // value of j unspecified
....
The value of j is unspecified to allow compilers to produce
optimal code. It is claimed that the difference between what can
be produced giving the compiler this freedom and requiring
"ordinary left-to-right evaluation" can be significant. I'm
unconvinced, but with innumerable compilers "out there" taking
advantage of the freedom and some people passionately defending
that freedom, a change would be difficult and could take decades
to penetrate to the distant corners of the C and C++ worlds.
</quote>
I think the common practice you refer to is not that common after
all. With today's abundance of CPU architectures the registers
jugglery can be found in every decent optimizer, which makes
undefined order of evaluation even less common and more undefined.
Alex
.
- References:
- Same code and different result, Why?
- From: Lorry Astra
- Re: Same code and different result, Why?
- From: Alex Blekhman
- Re: Same code and different result, Why?
- From: Tommy
- Re: Same code and different result, Why?
- From: Ulrich Eckhardt
- Re: Same code and different result, Why?
- From: Tommy
- Re: Same code and different result, Why?
- From: Alex Blekhman
- Re: Same code and different result, Why?
- From: Tommy
- Same code and different result, Why?
- Prev by Date: Re: Same code and different result, Why?
- Next by Date: localeconv() failing after _configthreadlocale()
- Previous by thread: Re: Same code and different result, Why?
- Next by thread: Re: Same code and different result, Why?
- Index(es):
Relevant Pages
|
Loading