Undefined behaviour

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • destination
    New Member
    • Dec 2009
    • 34

    Undefined behaviour

    why a[i]=i++ is an UB. here the value of i has changed only once.
    also i think there is no ambiguity in the value of i because in assignment operator the RHS part is evaluated and then assigned to LHS . so why this is an UB.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The undefined behaviour you are referring to here is
    Accessing any variable more than once between sequence points where one (or more) of those access is for modification is undefined behaviour.

    In the expression

    a[i] == i++;

    (note I have added a ;) the only sequence point is at the end of the expression on ;. Therefore this expression falls foul of the moratorium of accessing variables more then once including a modifying access between sequence points since i is accessed twice, once for read by the [] operator in a[i] and once for write by the ++ operator in i++.

    Comment

    Working...