undefined behaviour

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dan Cernat

    undefined behaviour

    Someone posted earlier an example that looked like taken from the
    standard. It was: i = 7, i++, i++; and the result should be 9 (i = 9
    in the end)
    I thought that the compiler isn't required to do the assignement until
    the right hand side expression is fully evaluated. Apparently, the
    above expression is handled as:
    i = 7;
    i++;
    i++;
    which I don't think is right. Could someone explain, please?

    /dan
  • Josh Sebastian

    #2
    Re: undefined behaviour

    On Tue, 11 Nov 2003 08:24:28 -0800, Dan Cernat wrote:
    [color=blue]
    > Someone posted earlier an example that looked like taken from the
    > standard. It was: i = 7, i++, i++; and the result should be 9 (i = 9
    > in the end)
    > I thought that the compiler isn't required to do the assignement until
    > the right hand side expression is fully evaluated. Apparently, the
    > above expression is handled as:
    > i = 7;
    > i++;
    > i++;
    > which I don't think is right. Could someone explain, please?[/color]

    Yes, it's handled like that. You're almost right about assignment:
    operator= has higher precedence than operator, so it's the same as if you
    wrote

    (i=7),i++,i++

    The assignment happens first, followed by a post-increment, followed by a
    post-increment. The comma operator inserts a sequence point, so the
    behavior isn't undefined.

    Josh

    Comment

    Working...