> Maybe you are correct here, it's neither a bug nor as designed, it's just 
> undefined, which means that other behavior could be taken under different 
> machines, and thus different results. 


A concern is that, while your analysis is interesting, you may risk misleading 
learners when you engage in discussion of this without peppering it with 
caveats and warnings; I'd suggest a large, bold disclaimer at the top rather 
than a footnote at the end, to indicate that the behaviour you are describing 
is not mandated by any standard and could be different in any of these 
situations (and more):
- with other compilers
- with other versions of GCC
- with the _same_ version of GCC on other OSes
- with the same version of GCC with different optimisations turned on
- with the same GCC, optimisations and OS on a different computer
- with the same GCC, optimisations, OS and even computer, run twice in a row on 
the same computer. 
- with the same _compiled binary_, run twice on the same computer. 
- randomly 
- on Tuesdays
- when there is a full moon

This variability of the behaviour seems to be completely permitted by the 
standard. The practical upshot of this is that as a programmer you should never 
rely on such behaviour (I think a good compiler can warn you if you do). 

There is a good treatment of this topic on Stack Overflow: 
http://stackoverflow.com/questions/340282/c-mystery - in particular answers by 
phihag and Michael Burr, who both point out the specific section of the 
standard that says that the behaviour is undefined (the latter explains what 
"undefined" means). I agree with Michael's sentiment:

"I think trying to explain what the compiler is doing...  is not particularly 
helpful, as that misses the point. The compiler could be doing almost anything; 
in fact, it's likely that the compiler may reach a different result when it's 
run using differing optimization options (or may produce code that crashes - 
who knows?)."

Barış

On 16 Aug 2011, at 23:00, Amahdy <[email protected]> wrote:

> k=++j + (++j + ++j); it gives 15!
> 
> As explained before, here is the compiler behavior that lead to this output:
> 
> j=2
> Divide the long expression into two branches: ++j AND (++j + ++j)
> 
> The first branch: j=j+1, then the result is found in j
> The second branch: divide into two subbranches, ++j AND ++j
> 
> The first branch: j=3 and its result is found in j
> The second branch:
>    The first subbranch: j=j+1, then the result is found in j
>    The second subbranch: j=j+1, then the result is found in j
> 
> The first branch: j=3 and its result is found in j
> The second branch:
>    The first subbranch: j=4 and its result is found in j
>    The second subbranch: j=5 and its result is found in j
> 
> The first branch: j=5
> The second branch: Add(The first subbranch: {j=5} , The second subbranch: 
> {j=5}), which gives 10
> 
> The result: Add(The first branch: {j=5}, 10), which gives 15
> 
> 
> 
> I agree, and the 'deep language specification' here is that this
> behaviour is undefined.
> 
> Maybe you are correct here, it's neither a bug nor as designed, it's just 
> undefined, which means that other behavior could be taken under different 
> machines, and thus different results. 
> 
> 
> -- Amahdy
> www.amahdy.net
> 
> 
> 
> On Tue, Aug 16, 2011 at 12:09, Paul Smith <[email protected]> wrote:
> On Tue, Aug 16, 2011 at 1:03 AM, Amahdy <[email protected]> wrote:
> > I don't agree with you +Paul, one of the essential learning curves IMHO, is
> > to learn the compiler behavior and the deep language specifications and
> > details.
> 
> I agree, and the 'deep language specification' here is that this
> behaviour is undefined.
> 
> --
> You received this message because you are subscribed to the Google Groups 
> "google-codejam" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/google-code?hl=en.
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "google-codejam" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/google-code?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"google-codejam" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-code?hl=en.

Reply via email to