Talk:Undefined behavior

From Esolang
Jump to: navigation, search

What's wrong with the C code given here? I would have thought that it would just set a to a*b and then add 1 to both a and b?

I think it's the opposite, actually: it always adds 1 to a and b and then sets a to the new value of a*b. The compiler needs to evaluate (a++ * b++) before it can set a, so it does that first, and in the process it increments a and b.
Regardless, it's a poor example even if it does turn out to be undefined. I'll replace it with a = b++ * b--. This is undefined because the arguments to * can be evaluated in either order: the compiler can decrement b first (setting a to b*(b-1)) or increment it first (setting a to (b+1)*b). Snowyowl (talk) 12:23, 28 February 2013 (UTC)
In the expression a = a++ * b++, it is undefined whether a++ or b++ will be evaluated first.--AnotherTest (talk) 12:33, 28 February 2013 (UTC)
True, but this isn't undefined behaviour because no object is modified more than once before the next sequence point. It's just an implementation detail. It doesn't make a difference if b++ is evaluated before a++ - they could even be evaluated simultaneously and it would give the same result. Snowyowl (talk) 12:58, 28 February 2013 (UTC)

I take back my previous post. a = a++ * b++ is undefined, but not for that reason. It's because the "a++" isn't required to write the new value of a before the "a=..." runs. The code snippet could easily set a to (a+1)(b+1) and then increment a and b. Snowyowl (talk) 15:02, 28 February 2013 (UTC)

Magic: the Gathering

=== Demons In Your Nose {UB} Instant ===

If there are any Demons in your nose, you may cast them. Each of those spells gains flying.
--Zzo38 (talk) 23:42, 5 February 2017 (UTC)