Validity of several ++operators on the same line

Discussion in 'C++' started by Kufa, Jan 16, 2007.

  1. Kufa

    Kufa Guest

    Hello,

    I am wondering of the validity of those lines, not meaning i'm using
    them, but i cant point out in the norm if they are legal, and in such a
    case the expected behaviour.

    int a = 2;
    int b = (++a) * ((++a)+2);
    printf( "%d %d", ++b, ++b );


    They compiled in Commeau, but that doesn't mean it's legal :)
    Any help or suggestion is welcome!
    Thanks,

    /kufa
     
    Kufa, Jan 16, 2007
    #1
    1. Advertising

  2. Kufa

    Kai-Uwe Bux Guest

    Kufa wrote:

    > I am wondering of the validity of those lines, not meaning i'm using
    > them, but i cant point out in the norm if they are legal, and in such a
    > case the expected behaviour.
    >
    > int a = 2;
    > int b = (++a) * ((++a)+2);
    > printf( "%d %d", ++b, ++b );


    The behavior is undefined as per clause [5/4] of the standard.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jan 16, 2007
    #2
    1. Advertising

  3. On Jan 16, 1:25 pm, "Kufa" <> wrote:
    > Hello,
    >
    > I am wondering of the validity of those lines, not meaning i'm using
    > them, but i cant point out in the norm if they are legal, and in such a
    > case the expected behaviour.
    >
    > int a = 2;
    > int b = (++a) * ((++a)+2);
    > printf( "%d %d", ++b, ++b );
    >
    > They compiled in Commeau, but that doesn't mean it's legal :)
    > Any help or suggestion is welcome!


    No, they are not legal. If in doubt just try to evaluate the expression
    in your head. Remember that there are no rules that say the just
    because A comes before B in an expression A will be evaluated first.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 16, 2007
    #3
  4. Kufa

    Kufa Guest


    > No, they are not legal. If in doubt just try to evaluate the expression
    > in your head. Remember that there are no rules that say the just
    > because A comes before B in an expression A will be evaluated first.


    Well i'd think this is legal with undefined behaviour. Can you point me
    somewhere in the norm that'd say it's not legal, really can't find
    anything.

    Thanks,

    /kUfa
     
    Kufa, Jan 16, 2007
    #4
  5. Kufa

    Rolf Magnus Guest

    Kufa wrote:

    > Hello,
    >
    > I am wondering of the validity of those lines, not meaning i'm using
    > them, but i cant point out in the norm if they are legal, and in such a
    > case the expected behaviour.
    >
    > int a = 2;



    > int b = (++a) * ((++a)+2);


    This one has undefined behavior, because a is modified twice without a
    sequence point in between.

    > printf( "%d %d", ++b, ++b );


    Undefined too, similar to the previous one.

    > They compiled in Commeau, but that doesn't mean it's legal :)


    Well, it's not a syntacital error. A compiler doesn't need to complain about
    it.
     
    Rolf Magnus, Jan 16, 2007
    #5
  6. Kufa

    Kufa Guest

    So to sum up and check i did not misunderstand you, it's legal, but
    undefined behaviour ?

    Thanks for your answers!

    /Kufa
     
    Kufa, Jan 16, 2007
    #6
  7. Kufa

    Andre Kostur Guest

    "Kufa" <> wrote in news:1168952389.509578.101930@
    38g2000cwa.googlegroups.com:

    > So to sum up and check i did not misunderstand you, it's legal, but
    > undefined behaviour ?


    That depends on what you define as "legal". In my definition, "legal" code
    doesn't invoke Undefined Behaviour. If by "legal" you mean "syntactically
    correct", then yes it's legal.
     
    Andre Kostur, Jan 16, 2007
    #7
  8. Kufa

    red floyd Guest

    Kufa wrote:
    > So to sum up and check i did not misunderstand you, it's legal, but
    > undefined behaviour ?
    >
    > Thanks for your answers!
    >
    > /Kufa
    >

    Kai-Uwe Bux already told you where to look in the standard. 5/4.

    If you are unfamiliar with that notation, it means paragraph 4 of
    section 5, not section 5.4.
     
    red floyd, Jan 16, 2007
    #8
  9. Kufa

    Noah Roberts Guest

    Kufa wrote:
    > > No, they are not legal. If in doubt just try to evaluate the expression
    > > in your head. Remember that there are no rules that say the just
    > > because A comes before B in an expression A will be evaluated first.

    >
    > Well i'd think this is legal with undefined behaviour.


    "Undefined behavior" means the program is not well formed, hense
    illegal.
     
    Noah Roberts, Jan 16, 2007
    #9
  10. Kufa

    Pete Becker Guest

    Noah Roberts wrote:
    > Kufa wrote:
    >>> No, they are not legal. If in doubt just try to evaluate the expression
    >>> in your head. Remember that there are no rules that say the just
    >>> because A comes before B in an expression A will be evaluated first.

    >> Well i'd think this is legal with undefined behaviour.

    >
    > "Undefined behavior" means the program is not well formed, hense
    > illegal.
    >


    Does that mean I'll be arrested for writing code like that?

    Seriously, according to the standard, programs are well-formed or
    ill-formed, and some well-formed programs have undefined behavior. It
    doesn't say anything about legal or illegal. As always, using undefined
    terms leads to confusion.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
     
    Pete Becker, Jan 16, 2007
    #10
  11. Kufa

    Ron Natalie Guest

    Noah Roberts wrote:
    > Kufa wrote:
    >>> No, they are not legal. If in doubt just try to evaluate the expression
    >>> in your head. Remember that there are no rules that say the just
    >>> because A comes before B in an expression A will be evaluated first.

    >> Well i'd think this is legal with undefined behaviour.

    >
    > "Undefined behavior" means the program is not well formed, hense
    > illegal.
    >

    Nope, "undefined behavior" does not mean the program is not well
    formed. An ill-formed program requires a diagnostic. UB does
    not.
     
    Ron Natalie, Jan 17, 2007
    #11
  12. Kufa

    Ivan Novick Guest

    Kufa wrote:
    > So to sum up and check i did not misunderstand you, it's legal, but
    > undefined behaviour ?
    >
    > Thanks for your answers!
    >
    > /Kufa
    >


    To sum up, if you write code that changes the same variable twice on the
    same line you are not guaranteed what the code will do. i.e. its code
    that should not be written. Also if you add -Wall to your gcc compile
    it will warn you against doing this.

    --
    Ivan
    http://www.0x4849.net
     
    Ivan Novick, Jan 18, 2007
    #12
    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. Martin Burger
    Replies:
    3
    Views:
    563
    Roland
    Jun 30, 2005
  2. Zhou Lei

    XML 2 XHTML validity...

    Zhou Lei, Mar 3, 2005, in forum: XML
    Replies:
    2
    Views:
    467
    Zhou Lei
    Mar 5, 2005
  3. Jimmy zhang

    comment on the validity of xpath

    Jimmy zhang, Jul 26, 2005, in forum: XML
    Replies:
    1
    Views:
    334
  4. Replies:
    10
    Views:
    566
    Daniel T.
    Oct 20, 2006
  5. Victor \Zverok\ Shepelev

    RDOC: several related modules in several C files

    Victor \Zverok\ Shepelev, Mar 6, 2007, in forum: Ruby
    Replies:
    3
    Views:
    188
    Max Lapshin
    Mar 16, 2007
Loading...

Share This Page