undefined behaviour

Discussion in 'C++' started by Dan Cernat, Nov 11, 2003.

  1. Dan Cernat

    Dan Cernat Guest

    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
     
    Dan Cernat, Nov 11, 2003
    #1
    1. Advertising

  2. On Tue, 11 Nov 2003 08:24:28 -0800, Dan Cernat wrote:

    > 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?


    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
     
    Josh Sebastian, Nov 11, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. marbac

    Undefined behaviour

    marbac, Jul 13, 2004, in forum: C++
    Replies:
    48
    Views:
    1,342
    Default User
    Jul 20, 2004
  2. JKop
    Replies:
    1
    Views:
    282
    Andrew Koenig
    Aug 4, 2004
  3. Mantorok Redgormor
    Replies:
    70
    Views:
    1,766
    Dan Pop
    Feb 17, 2004
  4. VK
    Replies:
    45
    Views:
    607
    Dr John Stockton
    Sep 12, 2006
  5. -Lost
    Replies:
    13
    Views:
    373
    Richard Cornford
    Jan 31, 2007
Loading...

Share This Page