473,503 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is preincrementing the same variable twice valid c++?

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

Mar 1 '07 #1
7 1524
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.
Mar 1 '07 #2
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)
Mar 1 '07 #3
On Mar 1, 1:30 pm, Pete Becker <p...@versatilecoding.comwrote:
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

Mar 1 '07 #4
* Erik Wikström:
On Mar 1, 1:30 pm, Pete Becker <p...@versatilecoding.comwrote:
>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?
Mar 1 '07 #5
Erik Wikström wrote:
On Mar 1, 1:30 pm, Pete Becker <p...@versatilecoding.comwrote:
>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)
Mar 1 '07 #6
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.
Mar 1 '07 #7
Erik Wikström wrote:
On Mar 1, 1:30 pm, Pete Becker <p...@versatilecoding.comwrote:
>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.

Mar 1 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
6421
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
15
3576
by: Rick | last post by:
Hi, Does deleting an object more than one times incur undefined behavior? I think it doesn't but just making sure... thanks Rick
72
4124
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and...
3
1620
by: dei1c3 | last post by:
I've created a WebUserControl which is Composite Control. It includes several controls including a few RangeValidator controls and a ValidationSummary control. The WebUserControl works properly...
2
1616
by: Kiuhnm | last post by:
I am having trouble with the function Add4Ptrs_v2. I thought "twice< add_pointer<_>, _ >" was a perfectly valid metafunction, but the following code does not work: >>>> #include <iostream> ...
25
9492
by: Sourav | last post by:
Suppose I have a code like this, #include <stdio.h> int *p; void foo(int); int main(void){ foo(3); printf("%p %d\n",p,*p);
2
2106
by: Eric Lilja | last post by:
I'm looking at an assignment where the students are expected to write a class with a given purpose. According to the assignment, you should be able to do this with instances of the class:...
148
5405
by: onkar | last post by:
Given the following code & variable i . int main(int argc,char **argv){ int i; printf("%d\n",i); return 0; } here i is allocated from bss or stack ?
14
5799
by: paresh | last post by:
Is this the valid C statement. int a,b,c; c = 5; <<< a = b = c; Can anyone throw the light on this. -Paresh
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.