c - Assignment operator sequencing in C11 expressions -


introduction

the c11 standard (iso/iec 9899:2011) has introduced new definition of side effect sequencing within expression (see related question). sequence point concept has been complemented sequenced before , sequenced after relations basis definitions.

section 6.5 "expressions", point 2 says:

if side effect on scalar object unsequenced relative either different side effect on same scalar object or value computation using value of same scalar object, behavior undefined. if there multiple allowable orderings of subexpressions of expression, behavior undefined if such unsequenced side effect occurs in of orderings.

later on, section 6.5.16 "assignment operators", point 3 states:

the side effect of updating stored value of left operand sequenced after value computations of left , right operands. evaluations of operands unsequenced.

problem

the first quoted paragraph (6.5/2) supported 2 examples (same in c99 standard):

first example

a[i++] = i;  //! undefined a[i] = i;    //  allowed 

this can explained definitions:

  1. if side effect on scalar object unsequenced relative (...) value computation using value of same scalar object, behavior undefined. (6.5/2),
  2. the evaluations of operands unsequenced. [within assignment] (6.5.16/3).

so, side effect of i++ (lhs) unsequenced i (rhs), gives undefined behaviour.

second example

i = ++i + 1; //! undefined = + 1;   //  allowed 

this code, however, seems result in defined behaviour in both given cases as:

  1. the side effect of updating stored value of left operand sequenced after value computations of left , right operands.

so, execution of ++i + 1 shall precede side effect of updating i, means there not a side effect on scalar object unsequenced relative either different side effect on same scalar object or value computation using value of same scalar object.

question

it easy explain these examples terms , definitions presented c99 standard (see related question). why i = ++i + 1 undefined according c11's terminology?

update

i changing answer here, not defined in c11 although in c++11. key here result of ++i not lvalue , therefore not require lvalue-to-rvalue conversion after ++i evaluated , can not assured result of ++i read afterwards. different c++ , defect report linked hinges on critical fact:

[...] lvalue expression ++i , lvalue-to-rvalue conversion on result. guarantees incrementation side-effect sequenced before computation of addition operation[...]

we can see going c11 draft standard section 6.5.3.1 prefix increment , decrement operators says:

[...]the expression ++e equivalent (e+=1).[...]

and section 6.5.16 assignment operators says (emphasis mine going forward):

an assignment operator stores value in object designated left operand. assignment expression has value of left operand after assignment,111 but not lvalue.[...]

and footnote 111 says:

the implementation permitted read object determine value not required to, when object has volatile-qualified type.

there no requirement read object determine it's value if volatile.

original answer

as far can tell defined , example removed c++ draft standard uses similar language. can see in 637. sequencing rules , example disagree says:

the following expression still listed example of undefined behavior:

i = ++i + 1; 

however, appears new sequencing rules make expression well-defined:

and resolution strike prefix example , use postfix example instead undefined:

change example in 1.9 [intro.execution] paragraph 16 follows:

i = ++i i++ + 1; // behavior undefined


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -