Unusual operator behavior

Discussion in 'C++' started by Karlo Basic, Nov 10, 2003.

  1. Karlo Basic

    Karlo Basic Guest

    Greetings!
    I'm wondering how do the expressions in the following piece of code
    evaluate and why:

    #include <iostream>
    using namespace std;

    int main()
    {
    int n = 5, p;
    n = n + n++;
    cout << "n = " << n << endl;
    n = 5;
    p = n + n++;
    cout << "p = " << p << endl;

    return 0;
    }
    I compiled it both with g++ and bcc32 and the results are identical:
    n = 11
    p = 10
    The first result seems obvious. But I don't know what's going on with
    the evaluation of the p variable. I am really interested on how it
    works. Thank you in advance,
    Karlo.
     
    Karlo Basic, Nov 10, 2003
    #1
    1. Advertising

  2. Karlo Basic wrote:
    > I'm wondering how do the expressions in the following piece of code
    > evaluate and why:
    >
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > int n = 5, p;
    > n = n + n++;


    This expression modifies the stored value of 'n' more than once.
    Undefined behavior.

    > cout << "n = " << n << endl;
    > n = 5;
    > p = n + n++;


    This expression modifies 'n' and at the same time reads its old value
    for a purpose other than determining its new value. Undefined behavior.

    > cout << "p = " << p << endl;
    >
    > return 0;
    > }
    > I compiled it both with g++ and bcc32 and the results are identical:
    > n = 11
    > p = 10
    > The first result seems obvious. But I don't know what's going on with
    > the evaluation of the p variable. I am really interested on how it
    > works.


    There's no way to predict or explain how it will work. Both expressions
    result in undefined behavior.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Nov 10, 2003
    #2
    1. Advertising

  3. Karlo Basic wrote:
    >
    > Greetings!
    > I'm wondering how do the expressions in the following piece of code
    > evaluate and why:
    >
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > int n = 5, p;
    > n = n + n++;


    modifying a variable twice between sequence points -> undefined behaviour

    > cout << "n = " << n << endl;
    > n = 5;
    > p = n + n++;
    > cout << "p = " << p << endl;
    >
    > return 0;
    > }
    > I compiled it both with g++ and bcc32 and the results are identical:
    > n = 11
    > p = 10
    > The first result seems obvious.


    Not really.

    > But I don't know what's going on with
    > the evaluation of the p variable. I am really interested on how it
    > works.


    There is no correct answer. The compiler can do anything it wants.
    Your program has produced undefined behaviour and it is pointless
    to think about a 'correct solution' since there is none.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Nov 10, 2003
    #3
  4. The relevant part of the standard is Section 5 paragraph 4

    Except where noted, the order of evaluation of operands of individual
    operators and subexpressions of indi-vidual expressions, and the order
    in which side effects take place, is unspecified.53) Between the
    previous and next sequence point a scalar object shall have its stored
    value modified at most once by the evaluation of an expression.
    Furthermore, the prior value shall be accessed only to determine the
    value to be stored. The requirements of this paragraph shall be met for
    each allowable ordering of the subexpressions of a full expression;
    otherwise the behavior is undefined. [Example:
    i = v[i++]; // the behavior is unspecified
    i = 7, i++, i++; // i becomes 9
    i = ++i + 1; // the behavior is unspecified
    i = i + 1; // the value of i is incremented
    —end example]
     
    Tim Threlfall, Nov 10, 2003
    #4
  5. Karlo Basic

    A Guest


    > I'm wondering how do the expressions in the following piece of code
    > evaluate and why:
    >
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > int n = 5, p;
    > n = n + n++;
    > cout << "n = " << n << endl;
    > n = 5;
    > p = n + n++;
    > cout << "p = " << p << endl;
    >
    > return 0;
    > }
    > I compiled it both with g++ and bcc32 and the results are identical:
    > n = 11
    > p = 10
    > The first result seems obvious. But I don't know what's going on with
    > the evaluation of the p variable. I am really interested on how it
    > works. Thank you in advance,
    > Karlo.


    n = 5;
    p = n + n++; // EASY! add 5 and 5 and assign it to p and then increment n
    (postfix increment)
    cout << "p = " << p << endl;

    Regards,
    A
     
    A, Nov 11, 2003
    #5
  6. A wrote:
    >
    > > I'm wondering how do the expressions in the following piece of code
    > > evaluate and why:
    > >
    > > #include <iostream>
    > > using namespace std;
    > >
    > > int main()
    > > {
    > > int n = 5, p;
    > > n = n + n++;
    > > cout << "n = " << n << endl;
    > > n = 5;
    > > p = n + n++;
    > > cout << "p = " << p << endl;
    > >
    > > return 0;
    > > }
    > > I compiled it both with g++ and bcc32 and the results are identical:
    > > n = 11
    > > p = 10
    > > The first result seems obvious. But I don't know what's going on with
    > > the evaluation of the p variable. I am really interested on how it
    > > works. Thank you in advance,
    > > Karlo.

    >
    > n = 5;
    > p = n + n++; // EASY! add 5 and 5 and assign it to p and then increment n
    > (postfix increment)


    Not at all.
    The compiler coud also do.

    Take the value of n 5
    increment n
    Take the value of n 6
    Add both numbers

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Nov 11, 2003
    #6
  7. Karlo Basic

    Sinora Guest

    Here is what is going on here:

    n++ evaluates the expression first and add 1 to it. Therefore

    n = n + n++ (= 5 + 5) = 10 after evaluation 1 is added to n therefore n =
    11
    p = n+n++ (= 5+5) = 10, n is = 6 after this expression but p is still 10.

    In short,

    ++n increment the n first
    n++ evaluate the expression first

    Hope it explained your confusion.
    Dev

    "Karlo Basic" <> wrote in message
    news:bonjh2$1gcb7p$-berlin.de...
    > Greetings!
    > I'm wondering how do the expressions in the following piece of code
    > evaluate and why:
    >
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > int n = 5, p;
    > n = n + n++;
    > cout << "n = " << n << endl;
    > n = 5;
    > p = n + n++;
    > cout << "p = " << p << endl;
    >
    > return 0;
    > }
    > I compiled it both with g++ and bcc32 and the results are identical:
    > n = 11
    > p = 10
    > The first result seems obvious. But I don't know what's going on with
    > the evaluation of the p variable. I am really interested on how it
    > works. Thank you in advance,
    > Karlo.
    >
     
    Sinora, Nov 12, 2003
    #7
  8. "Sinora" <> wrote...
    > Here is what is going on here:
    >
    > n++ evaluates the expression first and add 1 to it. Therefore


    Stop right here. Adding of 1 to 'n' does NOT have to happen at the
    same time as the insertion of the value of 'n' into the expression.

    As others have mentioned, the behaviour of this code is undefined.

    >
    > n = n + n++ (= 5 + 5) = 10 after evaluation 1 is added to n therefore n =
    > 11
    > p = n+n++ (= 5+5) = 10, n is = 6 after this expression but p is still 10.
    >
    > In short,
    >
    > ++n increment the n first
    > n++ evaluate the expression first
    >
    > Hope it explained your confusion.


    You are confused no less than the original poster. Read the posts
    of others.

    > Dev
    >
    > "Karlo Basic" <> wrote in message
    > news:bonjh2$1gcb7p$-berlin.de...
    > > Greetings!
    > > I'm wondering how do the expressions in the following piece of code
    > > evaluate and why:
    > >
    > > #include <iostream>
    > > using namespace std;
    > >
    > > int main()
    > > {
    > > int n = 5, p;
    > > n = n + n++;
    > > cout << "n = " << n << endl;
    > > n = 5;
    > > p = n + n++;
    > > cout << "p = " << p << endl;
    > >
    > > return 0;
    > > }
    > > I compiled it both with g++ and bcc32 and the results are identical:
    > > n = 11
    > > p = 10
    > > The first result seems obvious. But I don't know what's going on with
    > > the evaluation of the p variable. I am really interested on how it
    > > works. Thank you in advance,
    > > Karlo.
    > >

    >
    >
     
    Victor Bazarov, Nov 12, 2003
    #8
  9. "Sinora" <> wrote...
    > Here is what is going on here:
    >
    > n++ evaluates the expression first and add 1 to it. Therefore


    Stop right here. Adding of 1 to 'n' does NOT have to happen at the
    same time as the insertion of the value of 'n' into the expression.

    As others have mentioned, the behaviour of this code is undefined.

    >
    > n = n + n++ (= 5 + 5) = 10 after evaluation 1 is added to n therefore n =
    > 11
    > p = n+n++ (= 5+5) = 10, n is = 6 after this expression but p is still 10.
    >
    > In short,
    >
    > ++n increment the n first
    > n++ evaluate the expression first
    >
    > Hope it explained your confusion.


    You are confused no less than the original poster. Read the posts
    of others.

    > Dev
    >
    > "Karlo Basic" <> wrote in message
    > news:bonjh2$1gcb7p$-berlin.de...
    > > Greetings!
    > > I'm wondering how do the expressions in the following piece of code
    > > evaluate and why:
    > >
    > > #include <iostream>
    > > using namespace std;
    > >
    > > int main()
    > > {
    > > int n = 5, p;
    > > n = n + n++;
    > > cout << "n = " << n << endl;
    > > n = 5;
    > > p = n + n++;
    > > cout << "p = " << p << endl;
    > >
    > > return 0;
    > > }
    > > I compiled it both with g++ and bcc32 and the results are identical:
    > > n = 11
    > > p = 10
    > > The first result seems obvious. But I don't know what's going on with
    > > the evaluation of the p variable. I am really interested on how it
    > > works. Thank you in advance,
    > > Karlo.
    > >

    >
    >
     
    Victor Bazarov, Nov 12, 2003
    #9
  10. Sinora wrote:
    > Here is what is going on here:
    >
    > n++ evaluates the expression first and add 1 to it. Therefore
    >
    > n = n + n++ (= 5 + 5) = 10 after evaluation 1 is added to n therefore n =
    > 11
    > p = n+n++ (= 5+5) = 10, n is = 6 after this expression but p is still 10.
    >
    > In short,
    >
    > ++n increment the n first
    > n++ evaluate the expression first
    >
    > Hope it explained your confusion.


    The explanation is incorrect and can only add to the confusion.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Nov 12, 2003
    #10
  11. Karlo Basic

    Karlo Guest

    "Sinora" <> wrote in message
    news:bos90b$k4i$...
    > Here is what is going on here:
    >
    > n++ evaluates the expression first and add 1 to it. Therefore
    >
    > n = n + n++ (= 5 + 5) = 10 after evaluation 1 is added to n therefore n =
    > 11
    > p = n+n++ (= 5+5) = 10, n is = 6 after this expression but p is still 10.
    >
    > In short,
    >
    > ++n increment the n first
    > n++ evaluate the expression first
    >
    > Hope it explained your confusion.
    > Dev


    Well I did look it up in the C++ standard and it really says that this
    behaviour is undefined. So this explanation, although logical, doesn't quite
    do the trick.
    So I am asking again: why is it implemented this way? Are other programming
    languages behaving the same way with these kind of expressions? Isn't this
    undefined behaviour dangerous?
    Thanks,
    Karlo.
     
    Karlo, Nov 13, 2003
    #11
  12. Karlo wrote:
    >
    > "Sinora" <> wrote in message
    > news:bos90b$k4i$...
    > > Here is what is going on here:
    > >
    > > n++ evaluates the expression first and add 1 to it. Therefore
    > >
    > > n = n + n++ (= 5 + 5) = 10 after evaluation 1 is added to n therefore n =
    > > 11
    > > p = n+n++ (= 5+5) = 10, n is = 6 after this expression but p is still 10.
    > >
    > > In short,
    > >
    > > ++n increment the n first
    > > n++ evaluate the expression first
    > >
    > > Hope it explained your confusion.
    > > Dev

    >
    > Well I did look it up in the C++ standard and it really says that this
    > behaviour is undefined. So this explanation, although logical, doesn't quite
    > do the trick.
    > So I am asking again: why is it implemented this way?


    To open up a door for the compiler for optimizations.

    > Are other programming
    > languages behaving the same way with these kind of expressions?


    Don't know. Ask in a newgroup which discusses 'other programming languages'.

    > Isn't this
    > undefined behaviour dangerous?


    In principle yes. In practice: no. It is very rare that one does the
    above in real code. All those posted examples are academic and constructed
    to show the 'undefined behaviour'. In 20 years I still am waiting to ever
    write
    n = n + n++;
    or
    p = n + n++;

    And even if I find the need to do so, I would write:

    n *= 2;
    p = 2 * n++;

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Nov 13, 2003
    #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. Duke of Hazard

    newbie: unusual email output

    Duke of Hazard, Jan 21, 2004, in forum: Perl
    Replies:
    4
    Views:
    764
  2. Farooq Khan
    Replies:
    3
    Views:
    445
    Jim Cheshire [MSFT]
    Oct 24, 2003
  3. Marty McDonald

    SoapException - unusual behavior

    Marty McDonald, May 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    692
    Marty McDonald
    May 19, 2004
  4. =?Utf-8?B?SGVucnk=?=

    Unusual session_end behavior...

    =?Utf-8?B?SGVucnk=?=, Jan 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    383
    =?Utf-8?B?SGVucnk=?=
    Jan 12, 2005
  5. Replies:
    4
    Views:
    551
Loading...

Share This Page