Unpredictable nature of increment operators
  Home FAQ Contact Sign in
comp.lang.c++ only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.c++ Profile…
 Up
Unpredictable nature of increment operators         


Author: bintom
Date: May 8, 2008 17:16

Why doe the following C++ code behaves as it does?

int i;

i=1; cout << (++i)++; Output: 2
i=1; cout << ++(++i); Output: 3
i=1; cout << (i++)++; Output: Error. LValue required
i=1; cout << ++(i++); Output: Error. LValue required
11 Comments
Re: Unpredictable nature of increment operators         


Author: Ian Collins
Date: May 8, 2008 17:17

bintom wrote:

Once is enough!

--
Ian Collins.
no comments
Re: Unpredictable nature of increment operators         


Author: Martin York
Date: May 9, 2008 10:11

On May 8, 5:16 pm, bintom gmail.com> wrote:
> Why doe the following C++ code behaves as it does?
>
> int i;
>
> i=1; cout << (++i)++; Output: 2
> i=1; cout << ++(++i); Output: 3
> i=1; cout << (i++)++; Output: Error. LValue required
> i=1; cout << ++(i++); Output: Error. LValue required

Because code like that is so hard to read you would never actually
write it!

Also remember that it is illegal to modify a variable more than once
within the same expression [undefined behavior I believe](somebody
with a copy of the standard will be able to quote you chapter an verse
and the actual definition rather than my layman's wording).
no comments
Re: Unpredictable nature of increment operators         


Author: Victor Bazarov
Date: May 9, 2008 10:21

Martin York wrote:
> On May 8, 5:16 pm, bintom gmail.com> wrote:
>> Why doe the following C++ code behaves as it does?
>>
>> int i;
>>
>> i=1; cout << (++i)++; Output: 2
>> i=1; cout << ++(++i); Output: 3
>> i=1; cout << (i++)++; Output: Error. LValue
>> required i=1; cout << ++(i++); Output: Error.
>> LValue required
>
> Because code like that is so hard to read you would never actually
> write it!
>
> Also remember that it is illegal to modify a variable more than once
> within the same expression [undefined behavior I believe](somebody
> with a copy of the standard will be able to quote you chapter an verse
> and the actual definition rather than my layman's wording).
Show full article (1.25Kb)
no comments
Re: Unpredictable nature of increment operators         


Author: James Kanze
Date: May 10, 2008 07:08

On 9 mai, 19:21, "Victor Bazarov" comAcast.net> wrote:
> Martin York wrote:
>> On May 8, 5:16 pm, bintom gmail.com> wrote:
>>> Why doe the following C++ code behaves as it does?
>>> int i;
>>> i=1; cout << (++i)++; Output: 2
>>> i=1; cout << ++(++i); Output: 3
>>> i=1; cout << (i++)++; Output: Error. LValue
>>> required i=1; cout << ++(i++); Output: Error.
>>> LValue required
>> Because code like that is so hard to read you would never actually
>> write it!
>> Also remember that it is illegal to modify a variable more
>> than once within the same expression [undefined behavior I
>> believe](somebody with a copy of the standard will be able
>> to quote you chapter an verse and the actual definition
>> rather than my layman's wording).
> Only a variable of a built-in type,

No. The rule holds for all types.
Show full article (2.52Kb)
no comments
Re: Unpredictable nature of increment operators         


Author: Jerry Coffin
Date: May 10, 2008 07:47

In article , v.Abazarov@comAcast.net
says...

[ ... ]
> For example, it's not undefined behaviour to do
>
> f(i++) + ++i;
>
> IIRC, but the results are unspecified.

I believe this still gives undefined behavior. There are three sequence
points in this expression: before f is called, when f returns, and at
the end of the expression. The compiler can generate code that evaluates
the pre-increment, then the post-increment, then the function call.
There's no sequence point between the pre- and post-increment, so the
result is undefined behavior.
> And if your class has the
> operators ++() or ++(int) overloaded, the results are even well-
> defined and predictable. For 'int' they aren't...
Show full article (2.10Kb)
no comments
Re: Unpredictable nature of increment operators         


Author: Jerry Coffin
Date: May 10, 2008 08:01

In article ea1258da781c@i76g2000hsf.googlegroups.com>, james.kanze@gmail.com
says...
> On 9 mai, 19:21, "Victor Bazarov" comAcast.net> wrote:

[ ... ]
>> For example, it's not undefined behaviour to do
>
>> f(i++) + ++i;
>
> Yes it is, since there is no sequence point between the two
> incrementations. Sequence points only define a partial
> ordering: there is a sequence point before calling f, but that
> only establishes and ordering between i++ and the call to f; it
> doesn't establish any ordering between the two incrementations.

I believe it establishes _some_ ordering, but not enough[1]. In
particular, I believe the compiler is required to treat evaluation of a
function argument and calling the function as atomic -- i.e. once the
evaluation of any argument takes place, it must proceed to evaluated the
other arguments (if any) and then call the function.
Show full article (1.96Kb)
no comments
Re: Unpredictable nature of increment operators         


Author: Bo Persson
Date: May 11, 2008 01:56

Jerry Coffin wrote:
> In article > ea1258da781c@i76g2000hsf.googlegroups.com>, james.kanze@gmail.com
> says...
>> On 9 mai, 19:21, "Victor Bazarov" comAcast.net> wrote:
>
> [ ... ]
>
>>> For example, it's not undefined behaviour to do
>>
>>> f(i++) + ++i;
>>
>> Yes it is, since there is no sequence point between the two
>> incrementations. Sequence points only define a partial
>> ordering: there is a sequence point before calling f, but that
>> only establishes and ordering between i++ and the call to f; it
>> doesn't establish any ordering between the two incrementations.
>
> I believe it establishes _some_ ordering, but not enough[1]. In
> particular, I believe the compiler is required to treat evaluation ...
Show full article (2.52Kb)
no comments
Re: Unpredictable nature of increment operators         


Author: Old Wolf
Date: May 11, 2008 02:14

On May 11, 3:01 am, Jerry Coffin taeus.com> wrote:
>> On 9 mai, 19:21, "Victor Bazarov" comAcast.net> wrote:
>>> For example, it's not undefined behaviour to do
>>>> f(i++) + ++i;
>
> I believe the compiler is required to treat evaluation of a
> function argument and calling the function as atomic -- i.e. once the
> evaluation of any argument takes place, it must proceed to evaluated the
> other arguments (if any) and then call the function.

There is no such requirement. In the following code,
for example,:
foo( bar(), baz() ) + qux();

the order of calling functions could be:
baz, qux, bar, foo.

or any other ordering, so long as 'foo' comes after
both 'bar' and 'baz'.

The code of Victor Bazarov causes undefined behaviour
if 'i' is a builtin type, because there might not be
a sequence point between the two modifications of 'i'.
no comments
Re: Unpredictable nature of increment operators         


Author: Jerry Coffin
Date: May 11, 2008 08:05

In article <68nqmmF2u7p59U1@mid.individual.net>, bop@gmb.dk says...

[ ... ]
>> 1: Though it's open to some question. $1.9/8 says: "Once the
>> execution of a function begins, no expressions from the calling
>> function are evaluated until execution of the called function has
>> completed."
>>
>> I'm interpreting evaluating the arguments to a function as part of
>> execution of the function. If you choose to interpret it as a
>> completely separate act that happens before the function's
>> execution, then you're right -- no ordering is defined. At least in
>> this case, it doesn't make any real difference though -- the
>> overall result is undefined behavior either way.
>
> I think your second interpretation is the correct one. It means that
> ++i can be evalueated before or after calling f, but not during.

You could _certainly_ be right. As I said, in this case it doesn't
really make much difference, since either one gives undefined results.
Show full article (1.87Kb)
no comments
1 2