4 ms·
a[i] = i++ is undefined, even though a[i] = i and a[j] = i++ each do what they obviously should.
by erik_seaberg 5y ago
a[i] = i++ is undefined, even though a[i] = i and a[j] = i++ each do what they obviously should.
- random_upvoter 5y ago> a[i] = i++ And I would never excrete such a line of code without automatically thinking "hmmm.. does this really do what I think it does?"
- gjulianm 5y agoIf I see a[i] = i++ in a codebase I am immediately removing it as it looks ambiguous, possibly invalid and probably unpredictable.
- adrian_b 5y agoThis is just a very particular case of calling a function with such arguments that the result will depend on the order of evaluation for the arguments. This is an undefined operation in almost all programming languages.
- Aeolos 5y agoThat is not actually the case, a great many languages define the order of operations in a deterministic manner. There is no intrinsic reason why a[i] = i++ has to result in undefined behavior.
- scj 5y agoEven if the behaviour is defined, relying on defined non-obvious behaviours means relying on subsequent programmers to have read the spec.