Velocity Reviews > C++ > Can someone tell me why? (Unary pre and post increment operator

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

Andreas Sheriff
Guest
Posts: n/a

 09-24-2004

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?

--
WARNING:
DO NOT REPLY TO THIS EMAIL
Reply to me only on this newsgroup

JKop
Guest
Posts: n/a

 09-24-2004
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.

> int b;

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.

> a = 5;

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

PKH
Guest
Posts: n/a

 09-24-2004

"Andreas Sheriff" <(E-Mail Removed)> wrote in message
> 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?
>
> --
> WARNING:
> DO NOT REPLY TO THIS EMAIL
> Reply to me only on this newsgroup
>

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

Rolf Magnus
Guest
Posts: n/a

 09-24-2004
JKop wrote:

> 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.
>
>> int b;

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

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.

Tom Widmer
Guest
Posts: n/a

 09-24-2004
On Fri, 24 Sep 2004 00:53:55 -0700, "Andreas Sheriff"
<(E-Mail Removed)> wrote:

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

nebjy
Guest
Posts: n/a

 09-24-2004
Andreas Sheriff wrote:

> 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

Simon Stienen
Guest
Posts: n/a

 09-24-2004
nebjy <(E-Mail Removed)> wrote:
> Andreas Sheriff wrote:
>
>> 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.

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.
--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle

nebjy
Guest
Posts: n/a

 09-24-2004
Simon Stienen wrote:

> nebjy <(E-Mail Removed)> wrote:
>
>>Andreas Sheriff wrote:
>>
>>
>>>
>>> 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.

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

Simon Stienen
Guest
Posts: n/a

 09-24-2004
nebjy <(E-Mail Removed)> wrote:
> Simon Stienen wrote:
>
>> nebjy <(E-Mail Removed)> wrote:
>>
>>>Andreas Sheriff wrote:
>>>
>>>
>>>>
>>>> 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.

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

Ok, you won... I didn't think THAT far, sorry :S
--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle

Andrey Tarasevich
Guest
Posts: n/a

 09-24-2004
Andreas Sheriff wrote:
> 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.

--
Best regards,
Andrey Tarasevich