By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,404 Members | 1,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,404 IT Pros & Developers. It's quick & easy.

Unusual operator behavior

P: n/a
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.

Jul 19 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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

Jul 19 '05 #2

P: n/a


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
kb******@gascad.at
Jul 19 '05 #3

P: n/a
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]

Jul 19 '05 #4

P: n/a
A
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
Jul 19 '05 #5

P: n/a


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
kb******@gascad.at
Jul 19 '05 #6

P: n/a
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" <ka*****@email.si> wrote in message
news:bo*************@ID-212926.news.uni-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.

Jul 19 '05 #7

P: n/a
"Sinora" <s_*********@hotmail.com> 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" <ka*****@email.si> wrote in message
news:bo*************@ID-212926.news.uni-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.


Jul 19 '05 #8

P: n/a
"Sinora" <s_*********@hotmail.com> 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" <ka*****@email.si> wrote in message
news:bo*************@ID-212926.news.uni-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.


Jul 19 '05 #9

P: n/a
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

Jul 19 '05 #10

P: n/a
"Sinora" <s_*********@hotmail.com> wrote in message
news:bo**********@news.iastate.edu...
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.
Jul 19 '05 #11

P: n/a


Karlo wrote:

"Sinora" <s_*********@hotmail.com> wrote in message
news:bo**********@news.iastate.edu...
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
kb******@gascad.at
Jul 19 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.