Can someone tell me why? (Unary pre and post increment operator

A

Andreas Sheriff

Please evaluate the following snippet:

int a=5;
int b;
b = a++ + a;
Edit1->Text = b;
a = 5;
b = a++ + ++a;
Edit2->Text = b;

Can you tell me why the value of b assigned to Edit1->Text is 10
while the value of b assigned to Edit2->Text is 12?

And if you can, why isn't it documented?
 
J

JKop

Andreas Sheriff posted:
Please evaluate the following snippet:

int a=5;

Defines a variable called "a" of the type "int" and sets its value to 5.

Defines variable: Name = "b". Type = "int". Value = no particular value
b = a++ + a;

"b" could end up with two possible values here, either:

10

or

11

as the Standard doesn't dictate whether "a++" or "a" will be done first.

The variable "a" ends up with the value 6.
Edit1->Text = b;


The text will be either 10 or 11.



Sets "a"'s value to 5.

b = a++ + ++a;


"b" gets:

12

either by "5 + 7" or "6 + 6".

"a" becomes 7.

Edit2->Text = b;


Text is set to 12.

Can you tell me why the value of b assigned to Edit1->Text is 10
while the value of b assigned to Edit2->Text is 12?


You're welcome!


-JKop
 
P

PKH

Andreas Sheriff said:
Please evaluate the following snippet:

int a=5;
int b;
b = a++ + a;
Edit1->Text = b;
a = 5;
b = a++ + ++a;
Edit2->Text = b;

Can you tell me why the value of b assigned to Edit1->Text is 10
while the value of b assigned to Edit2->Text is 12?

And if you can, why isn't it documented?

Here's what I think happens:
int a=5;
int b;
b = a++ + a;
1) b = 5 + 5
2) a++ is done after the assignment, a = 6
Edit1->Text = b;
a = 5;
b = a++ + ++a;
1) ++a is done before the assignment, a = 6
2) b = 6 + 6
3) a++ is done after the assignment, a = 7
Edit2->Text = b;

PKH
 
R

Rolf Magnus

JKop said:
Andreas Sheriff posted:


Defines a variable called "a" of the type "int" and sets its value to 5.


Defines variable: Name = "b". Type = "int". Value = no particular value


"b" could end up with two possible values here, either:

10

or

11

Those are not the only possible results. a is read from and written to
without a sequence point in between. The behavior of that line is
undefined, so anything can happen. From the point of undefined behavior on,
it's of no use to discuss what any following code does.
 
T

Tom Widmer

Please evaluate the following snippet:

int a=5;
int b;
b = a++ + a;

The above both modifies a (a++) and separately reads its value (a).
Edit1->Text = b;
a = 5;
b = a++ + ++a;

The above modifies a twice.
Edit2->Text = b;

Can you tell me why the value of b assigned to Edit1->Text is 10
while the value of b assigned to Edit2->Text is 12?

And if you can, why isn't it documented?

Modifying a value more than once or modifying it and reading it except
as part of the modification without an intervening sequence point
results in undefined behaviour. So the program might generate any
result at all for the two lines in question, or might crash. Have a
look at the C faq on expressions:
http://www.eskimo.com/~scs/C-faq/s3.html

Tom
 
N

nebjy

Andreas said:
Please evaluate the following snippet:

int a=5;
int b;
b = a++ + a;
Edit1->Text = b;
a = 5;
b = a++ + ++a;
Edit2->Text = b;

Can you tell me why the value of b assigned to Edit1->Text is 10
while the value of b assigned to Edit2->Text is 12?

And if you can, why isn't it documented?

It's a very interesting situation. It looks like b is being evaluated
from the right, leftwards. So in the first case, a=5, added to a++ which
returns 5 and then incremented (as opposed to ++a which increments first
and then returns that). So in the second case, 6 is returned which is
added to another 6 (which is then incremented but that makes no
difference). Giving 12.

However!

If you genuinely have code like this in your program, then you should
seriously consider rewriting it with a few more lines, for everyone's
sanity :)
 
S

Simon Stienen

nebjy said:
It's a very interesting situation. It looks like b is being evaluated
from the right, leftwards. So in the first case, a=5, added to a++ which
returns 5 and then incremented (as opposed to ++a which increments first
and then returns that). So in the second case, 6 is returned which is
added to another 6 (which is then incremented but that makes no
difference). Giving 12.

It looks like it is evaluated from left to right: Taking A (5) then
incrementing it to 6. Afterward A is incremented to 7 and then added to the
5, giving a result of 12.
 
N

nebjy

Simon said:
It looks like it is evaluated from left to right: Taking A (5) then
incrementing it to 6. Afterward A is incremented to 7 and then added to the
5, giving a result of 12.

True, but that doesn't hold for the first case. Evaluating from left to
right there gives 11, not 10 as the OP said.
 
S

Simon Stienen

nebjy said:
True, but that doesn't hold for the first case. Evaluating from left to
right there gives 11, not 10 as the OP said.

Ok, you won... I didn't think THAT far, sorry :S
 
A

Andrey Tarasevich

Andreas said:
Please evaluate the following snippet:

int a=5;
int b;
b = a++ + a;
Edit1->Text = b;
a = 5;
b = a++ + ++a;
Edit2->Text = b;

Can you tell me why the value of b assigned to Edit1->Text is 10
while the value of b assigned to Edit2->Text is 12?

Both 'a++ + a' expression and 'a++ + ++a' expression produce undefined
behavior. The former modifies 'a' and reads it value for unrelated
purpose. The latter modifies 'a' twice. Both violate requirements
specified in 5/4.

There's no way to predict what is assigned to 'b', if anything is
assigned at all. Your code is broken.
And if you can, why isn't it documented?

There's no point to document it more than it already is documented: the
code produces undefined behavior. End of story.
 
J

Jack Klein

Here's what I think happens:

Here's what I know happens. The behavior is specifically undefined by
the C++ language. Whatever does or does not happen is just as right
or wrong as anything else. The C++ language takes no responsibility
for the results.

The results are off-topic here because they are not a language issue.
They are, at best, a compiler-specific issue.
 

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

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top