Why is x=x++ undefined?
Posted
by
ugoren
on Programmers
See other posts from Programmers
or by ugoren
Published on 2012-06-19T07:24:02Z
Indexed on
2012/06/19
9:23 UTC
Read the original article
Hit count: 240
c
|language-design
It's undefined because the it modifies x
twice between sequence points. The standard says it's undefined, therefore it's undefined.
That much I know.
But why?
My understanding is that forbidding this allows compilers to optimize better. This could have made sense when C was invented, but now seems like a weak argument.
If we were to reinvent C today, would we do it this way, or can it be done better?
Or maybe there's a deeper problem, that makes it hard to define consistent rules for such expressions, so it's best to forbid them?
So suppose we were to reinvent C today. I'd like to suggest simple rules for expressions such as x=x++
, which seem to me to work better than the existing rules.
I'd like to get your opinion on the suggested rules compared to the existing ones, or other suggestions.
Suggested Rules:
- Between sequence points, order of evaluation is unspecified.
- Side effects take place immediately.
There's no undefined behavior involved. Expressions evaluate to this value or that, but surely won't format your hard disk (strangely, I've never seen an implementation where x=x++
formats the hard disk).
Example Expressions
x=x++
- Well defined, doesn't changex
.
First,x
is incremented (immediately whenx++
is evaluated), then it's old value is stored inx
.x++ + ++x
- Incrementsx
twice, evaluates to2*x+2
.
Though either side may be evaluated first, the result is eitherx + (x+2)
(left side first) or(x+1) + (x+1)
(right side first).x = x + (x=3)
- Unspecified,x
set to eitherx+3
or6
.
If the right side is evaluated first, it'sx+3
. It's also possible thatx=3
is evaluated first, so it's3+3
. In either case, thex=3
assignment happens immediately whenx=3
is evaluated, so the value stored is overwritten by the other assignment.x+=(x=3)
- Well defined, setsx
to 6.
You could argue that this is just shorthand for the expression above.
But I'd say that+=
must be executed afterx=3
, and not in two parts (readx
, evaluatex=3
, add and store new value).
What's the Advantage?
Some comments raised this good point.
It's not that I'm after the pleasure of using x=x++
in my code. It's a strange and misleading expression. What I want is to be able to understand complicated expressions.
Normally, a complicated expression is no more than the sum of its parts. If you understand the parts and the operators combining them, you can understand the whole.
C's current behavior seems to deviate from this principle. One assignment plus another assignment suddenly doesn't make two assignments.
Today, when I look at x=x++
, I can't say what it does. With my suggested rules, I can, by simply examining its components and their relations.
© Programmers or respective owner