Is it standard C++ code?

Discussion in 'C++' started by ljh131, Nov 11, 2005.

  1. ljh131

    ljh131 Guest

    i found something wrong code.

    int i = 0;
    i = i++;

    is it standard c++? peoples say i == 1 in most compilers but anothers
    not the same. if so, may be it's not standard code, however i want know
    positively.
     
    ljh131, Nov 11, 2005
    #1
    1. Advertising

  2. ljh131

    Ron Natalie Guest

    ljh131 wrote:
    > i found something wrong code.
    >
    > int i = 0;
    > i = i++;
    >
    > is it standard c++? peoples say i == 1 in most compilers but anothers
    > not the same. if so, may be it's not standard code, however i want know
    > positively.
    >

    It's undefined behavior. You are attempting to change a variable
    twice between sequence points (once for the assignment and once
    for the side-effect of ++).
     
    Ron Natalie, Nov 11, 2005
    #2
    1. Advertising

  3. ljh131 wrote:
    > i found something wrong code.
    >
    > int i = 0;
    > i = i++;
    >
    > is it standard c++?


    Yes, in the sense that it will compile. However, the behaviour of
    that code is undefined, according to the Standard. So, the usual
    advice is: don't do that.

    > peoples say i == 1 in most compilers but anothers
    > not the same. if so, may be it's not standard code, however i want know
    > positively.
    >


    If the behaviour is undefined, there is nothing we can say about the
    outcome of executing that code from the Standard C++ point of view.

    V
     
    Victor Bazarov, Nov 11, 2005
    #3
  4. ljh131

    Heinz Ozwirk Guest

    "ljh131" <> schrieb im Newsbeitrag
    news:...
    >i found something wrong code.
    >
    > int i = 0;
    > i = i++;
    >
    > is it standard c++? peoples say i == 1 in most compilers but anothers
    > not the same. if so, may be it's not standard code, however i want know
    > positively.


    Yes, it is standard C++, but the behaviour is not defined by the standard.
    In the statement 'i = i++', there is no sequence point and i is modified
    more than once. Many compilers might create code such that finaly i == 1,
    but it is easy to imagine a sequence of code, that results in i == 0:

    1. Evaluate the expression i
    2. Increment i
    3. Assign the result of step 1 to i

    So remember what they tell the kids in TV -- "Don't do that at work"

    HTH
    Heinz
     
    Heinz Ozwirk, Nov 11, 2005
    #4
  5. ljh131

    Greg Comeau Guest

    In article <4374d35c$0$21945$-online.net>,
    Heinz Ozwirk <> wrote:
    >"ljh131" <> schrieb im Newsbeitrag
    >news:...
    >>i found something wrong code.
    >>
    >> int i = 0;
    >> i = i++;
    >>
    >> is it standard c++? peoples say i == 1 in most compilers but anothers
    >> not the same. if so, may be it's not standard code, however i want know
    >> positively.

    >
    >Yes, it is standard C++, but the behaviour is not defined by the standard.
    >In the statement 'i = i++', there is no sequence point and i is modified
    >more than once. Many compilers might create code such that finaly i == 1,
    >but it is easy to imagine a sequence of code, that results in i == 0:
    >
    > 1. Evaluate the expression i
    > 2. Increment i
    > 3. Assign the result of step 1 to i
    >
    >So remember what they tell the kids in TV -- "Don't do that at work"


    Right, the problem is whether the ++ side-effect takes place
    before or after the assignment. But we don't get only those
    two choices (which is bad enough) and hence as you say it is
    undefined behavior.
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Nov 11, 2005
    #5
    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. steve.leach

    How standard is the standard library?

    steve.leach, Apr 18, 2005, in forum: Python
    Replies:
    1
    Views:
    419
    Christos TZOTZIOY Georgiou
    Apr 18, 2005
  2. funkyj
    Replies:
    5
    Views:
    1,185
    funkyj
    Jan 20, 2006
  3. Replies:
    5
    Views:
    885
  4. Sriram Srinivasan
    Replies:
    13
    Views:
    596
    Benjamin Kaplan
    Nov 12, 2009
  5. Venks
    Replies:
    5
    Views:
    278
    Ken Bloom
    Dec 6, 2007
Loading...

Share This Page