Unusual operator behavior

K

Karlo Basic

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.
 
A

Andrey Tarasevich

Karlo said:
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.
 
K

Karl Heinz Buchegger

Karlo said:
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.
 
T

Tim Threlfall

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]
 
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
 
K

Karl Heinz Buchegger

A said:
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
 
S

Sinora

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
 
V

Victor Bazarov

Sinora said:
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.
 
V

Victor Bazarov

Sinora said:
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.
 
A

Andrey Tarasevich

Sinora said:
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.
 
K

Karlo

Sinora said:
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.
 
K

Karl Heinz Buchegger

Karlo said:
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++;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top