473,657 Members | 2,711 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 1532
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...@versatile coding.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...@versatile coding.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...@versatile coding.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...@versatile coding.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
6479
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 C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
15
3583
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
4190
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 c?
3
1627
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 in most situations, but I've discovered a problem when I place 2 or more of these controls on the same page. Basically, the ValidationSummary control on each of the WebUserControls seems to be displaying error messages for RangeValidators on ALL...
2
1626
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> #include <boost/type_traits.hpp> #include <boost/mpl/vector.hpp>
25
9522
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
2119
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: ++instance_of_class; instance_of_class++; (++instance_of_class)++; // Isn't this invalid C++? but not: ++(instance_of_class++);
148
5499
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
5811
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
8319
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8512
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8612
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4171
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2739
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 we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.