Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is preincrementing the same variable twice valid c++?

Reply
Thread Tools

Is preincrementing the same variable twice valid c++?

 
 
Eric Lilja
Guest
Posts: n/a
 
      03-01-2007
...or does it violate the rule that a variable may not be modified more
than once between two sequence points?

int n = 0;
std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;

- Eric

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      03-01-2007
Eric Lilja wrote:
> ..or does it violate the rule that a variable may not be modified more
> than once between two sequence points?
>
> int n = 0;
> std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;


It's undefined.
 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      03-01-2007
Eric Lilja wrote:
> ..or does it violate the rule that a variable may not be modified more
> than once between two sequence points?
>


There's a sequence point before each function call (i.e. before each <<
function), so that rule doesn't apply. But the order of evaluation of
arguments is unspecified, so you can't count on any particular order for
the various values of n.

> int n = 0;
> std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;
>
> - Eric
>



--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
Reply With Quote
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      03-01-2007
On Mar 1, 1:30 pm, Pete Becker <(E-Mail Removed)> wrote:
> Eric Lilja wrote:
> > ..or does it violate the rule that a variable may not be modified more
> > than once between two sequence points?

>
> There's a sequence point before each function call (i.e. before each <<
> function), so that rule doesn't apply. But the order of evaluation of
> arguments is unspecified, so you can't count on any particular order for
> the various values of n.


That's not true

> > int n = 0;
> > std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;

^^^^^^^^^^^^^^^^^
This has to execute before the << ' ' can, otherwise there won't be a
ostream as first parameter to the << ' '-call.

--
Erik Wikström


--
Erik Wikström

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-01-2007
* Erik Wikström:
> On Mar 1, 1:30 pm, Pete Becker <(E-Mail Removed)> wrote:
>> Eric Lilja wrote:
>>> ..or does it violate the rule that a variable may not be modified more
>>> than once between two sequence points?

>> There's a sequence point before each function call (i.e. before each <<
>> function), so that rule doesn't apply. But the order of evaluation of
>> arguments is unspecified, so you can't count on any particular order for
>> the various values of n.

>
> That's not true


What isn't true?


>>> int n = 0;
>>> std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;

> ^^^^^^^^^^^^^^^^^
> This has to execute before the << ' ' can, otherwise there won't be a
> ostream as first parameter to the << ' '-call.


Arguments can be evaluated before anything else happens.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      03-01-2007
Erik Wikström wrote:
> On Mar 1, 1:30 pm, Pete Becker <(E-Mail Removed)> wrote:
>> Eric Lilja wrote:
>>> ..or does it violate the rule that a variable may not be modified more
>>> than once between two sequence points?

>> There's a sequence point before each function call (i.e. before each <<
>> function), so that rule doesn't apply. But the order of evaluation of
>> arguments is unspecified, so you can't count on any particular order for
>> the various values of n.

>
> That's not true
>
>>> int n = 0;
>>> std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;

> ^^^^^^^^^^^^^^^^^
> This has to execute before the << ' ' can, otherwise there won't be a
> ostream as first parameter to the << ' '-call.
>


The order of the calls to operator << is well defined, but the order of
evaluation of the arguments is unspecified. The compiler can compute the
value of the first ++n, then the n, then the final ++n. It can also do
them in reverse order, or any other order.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      03-01-2007
Pete Becker wrote:
> Eric Lilja wrote:
>> ..or does it violate the rule that a variable may not be modified more
>> than once between two sequence points?
>>

>
> There's a sequence point before each function call (i.e. before each <<
> function), so that rule doesn't apply. But the order of evaluation of
> arguments is unspecified, so you can't count on any particular order for
> the various values of n.
>

That rule does apply. Since the function parameters may be evaluated
in any order, both ++n subexpressions may be executed before ANY
function call occurs. Hence you will have the undefined behavior or
twice modifying a value between sequence points. The rule requires
that be true for ANY ALLOWABLE ORDERING of the expression.
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      03-01-2007
Erik Wikström wrote:
> On Mar 1, 1:30 pm, Pete Becker <(E-Mail Removed)> wrote:
>> Eric Lilja wrote:
>>> ..or does it violate the rule that a variable may not be modified more
>>> than once between two sequence points?

>> There's a sequence point before each function call (i.e. before each <<
>> function), so that rule doesn't apply. But the order of evaluation of
>> arguments is unspecified, so you can't count on any particular order for
>> the various values of n.

>
> That's not true


Partially true.

>
>>> int n = 0;
>>> std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;

> ^^^^^^^^^^^^^^^^^
> This has to execute before the << ' ' can, otherwise there won't be a
> ostream as first parameter to the << ' '-call.
>


The real issue is that the compiler is free to execute both ++n
subexpressions in any order and at any time prior to the call
of the operator<< where they are parameters. Specifically it
can execute them BOTH before either operator << call ,causing
undefined behavior.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
same object share same static variable??? Mug C++ 4 09-03-2009 12:51 PM
a ifstream variable cannot open the same file twice? Xiaozhou.Yin@gmail.com C++ 1 11-06-2007 02:01 PM
twice(twice(x)) Kiuhnm C++ 2 04-01-2006 04:41 PM
Accessing a global variable when there is a local variable in the same name Mohanasundaram C Programming 44 08-23-2004 11:17 PM
Same user twice in the same aspx.. Rajesh.V ASP .Net Web Controls 0 08-06-2003 06:19 PM



Advertisments