473,854 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

to guru : strange C++ operator behaviour

hi,

I found an interesting thing in operator behaviour in C++ :

int i=1;
printf("%d",i++ + i++);

I think the value of the expression "i++ + i++" _must_ be 3, but all the
compilers I tested print 2.

Then I tried another example

std::vector<int > a;
a.push_back(1);
a.push_back(2);
std::vector<int >::iterator i = a.begin();
printf("%d",*i+ + + *i++);

Different compilers print different values : 2 or 3, thought it _must_ be 3.

Possibly, it is because of the compilers C compatibility, since iterators
are C++ construction and their behaviour therefore suits C++ standards,
while for the basic types compilers guarantee C behaviour.

Bjarne Stroustrup wrote in his book that
y = ++x; is equivalent to y = (x+=1);
while
y = x++; is equivalent to y = (t=x,x+=1,t);

Why do all the compilers incorrectly compute the expressions?

Many thanks in advance,
Dmitriy Iassenev.

Jul 19 '05 #1
36 3467
On Thu, 2 Oct 2003 19:55:37 +0300, "Dmitriy Iassenev"
<ia******@gsc-game.kiev.ua> wrote:
hi,

I found an interesting thing in operator behaviour in C++ :

int i=1;
printf("%d",i+ + + i++);

I think the value of the expression "i++ + i++" _must_ be 3, but all the
compilers I tested print 2.


This is an FAQ:

http://www.eskimo.com/~scs/C-faq/s3.html

Tom
Jul 19 '05 #2
WW
Dmitriy Iassenev wrote:
hi,

I found an interesting thing in operator behaviour in C++ :

int i=1;
printf("%d",i++ + i++);

I think the value of the expression "i++ + i++" _must_ be 3, but all
the compilers I tested print 2.


You think wrong. The above is undefined behavior. You try to change the
value of a variable more than once with an intervening sequence point. The
compiler is allowed to do whatever it wants, including formatting your
harddisk. Search for sequence point i+++++i on Google. One page is:

http://www.embedded.com/story/OEG20020625S0041

--
WW aka Attila
Jul 19 '05 #3

int i=1;
printf("%d",i++ + i++);

I think the value of the expression "i++ + i++" _must_ be 3, but all the
compilers I tested print 2.

Bjarne Stroustrup wrote in his book that
y = ++x; is equivalent to y = (x+=1);
while
y = x++; is equivalent to y = (t=x,x+=1,t);

Why do all the compilers incorrectly compute the expressions?


His statement agrees with your test perfectly, doesn't it? You get back 2,
which is 1+1, and after that value is computed, then i gets incremented
(twice). Using his notation,

y = x++ + x++; would be like

y = (t = x+x, x+=1, x+= 1, t);

Right?

-Howard
Jul 19 '05 #4

You think wrong. The above is undefined behavior. You try to change the
value of a variable more than once with an intervening sequence point. The compiler is allowed to do whatever it wants, including formatting your
harddisk. Search for sequence point i+++++i on Google. One page is:


Oh, good point. I forgot about sequence points.

But regardless of what "undefined behavior" means in the standard, you had
best not sell me a compiler that formats my hard drive if I screw up simple
code like this...I know a good lawyer! :-)

-Howard
Jul 19 '05 #5

"Howard" <al*****@hotmai l.com> wrote in message
news:bl******** @dispatch.conce ntric.net...

int i=1;
printf("%d",i++ + i++);

I think the value of the expression "i++ + i++" _must_ be 3, but all the
compilers I tested print 2.

Bjarne Stroustrup wrote in his book that
y = ++x; is equivalent to y = (x+=1);
while
y = x++; is equivalent to y = (t=x,x+=1,t);

Why do all the compilers incorrectly compute the expressions?


His statement agrees with your test perfectly, doesn't it? You get back

2, which is 1+1, and after that value is computed, then i gets incremented
(twice). Using his notation,

y = x++ + x++; would be like

y = (t = x+x, x+=1, x+= 1, t);

Right?


Wrong. modifying the same variable twice in the same expression in
undefined so anything could happen.
Jul 19 '05 #6
"Dmitriy Iassenev" <ia******@gsc-game.kiev.ua> wrote in message
news:bl******** **@news.lucky.n et...
hi,

I found an interesting thing in operator behaviour in C++ :

int i=1;
printf("%d",i++ + i++);


http://www.eskimo.com/~scs/C-faq/q3.3.html
Jul 19 '05 #7
"Howard" <al*****@hotmai l.com> wrote...

int i=1;
printf("%d",i++ + i++);

I think the value of the expression "i++ + i++" _must_ be 3, but all the
compilers I tested print 2.

Bjarne Stroustrup wrote in his book that
y = ++x; is equivalent to y = (x+=1);
while
y = x++; is equivalent to y = (t=x,x+=1,t);

Why do all the compilers incorrectly compute the expressions?


His statement agrees with your test perfectly, doesn't it? You get back

2, which is 1+1, and after that value is computed, then i gets incremented
(twice). Using his notation,

y = x++ + x++; would be like

y = (t = x+x, x+=1, x+= 1, t);

Right?


Nope. Modifying a value of an object twice between sequence points
causes undefined behaviour. Anything is allowed to happen.

The expression (x++ + x++) is NOT like (t = x+x, x+=2) because there
is NO sequence point in the former, whereas in the latter there is
one at the comma.

Victor
Jul 19 '05 #8
Many thanks to all of you,

I did find an answer in the FAQ that clarified the described situation.

Best regards,
Dmitriy Iassenev.
Jul 19 '05 #9
WW
Howard wrote:
You think wrong. The above is undefined behavior. You try to
change the value of a variable more than once with an intervening
sequence point. The compiler is allowed to do whatever it wants,
including formatting your harddisk. Search for sequence point
i+++++i on Google. One page is:


Oh, good point. I forgot about sequence points.

But regardless of what "undefined behavior" means in the standard,
you had best not sell me a compiler that formats my hard drive if I
screw up simple code like this...I know a good lawyer! :-)


That is outside of the topic of this newsgroup. It belongs to QoI (Quality
of Implementation) . ;-)

--
WW aka Attila
Jul 19 '05 #10

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

Similar topics

4
5616
by: Ben | last post by:
Hi all, I'm trying to figure out how how complex map, filter and reduce work based on the following piece of code from http://www-106.ibm.com/developerworks/linux/library/l-prog.html : bigmuls = lambda xs,ys: filter(lambda (x,y):x*y > 25, combine(xs,ys)) combine = lambda xs,ys: map(None, xs*len(ys), dupelms(ys,len(xs))) dupelms = lambda lst,n: reduce(lambda s,t:s+t, map(lambda l,n=n: *n, lst))
10
2319
by: Piotr Wyderski | last post by:
Hello, is it possible to reuse a friend operator which is defined inside a class? I'd like to obtain the following behaviour: class integer { integer operator +(signed long int v) const {
4
1677
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
2
8932
by: Olaf | last post by:
I have a frameset page witch contains the myFuc() function. The function is accessed from a page in one of the frames in the frameset. An example is shown below. <input onclick="javaScript:alert('document.forms(0)='+document.forms(0)); parent.myFunc(document.forms(0));" type="button" value="Open" name="Button" ID="Button"> The strange part is that the debug alert says that the document.forms(0) is an object så all seem to be well. But...
3
2366
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
2
1725
by: shaun roe | last post by:
Following up from my earlier post, where I pursued the line I outlined. Here is the MYFunc class implementation: MyFunc::MyFunc(int i):m_i(i){ ostringstream os; os<<"This number is "<<m_i; m_result = os.str(); }
19
2240
by: scroopy | last post by:
Is it impossible in C++ to create an assignment operator for classes with const data? I want to do something like this class MyClass { const int m_iValue; public: MyClass(int iVal):m_iValue(iVal){}
23
1799
by: Saizan | last post by:
Why subclassing bool from int either __invert__ or __neg__ haven't been overrided to produce a boolean negation? I suspect backwards compatibility or something alike, but I still wonder.. And since bool can't be subclassed, to have a type like bool but with boolean negation what do you suggest? A wrapper maybe? (I would use it so I can evaluate user-defined boolean expression creating istances of his/her variables in my namespace and...
8
1649
by: Chris LaVelle | last post by:
I don't know the easiest way to explain this so, I'm going to give an example of how it is today....and what I'm trying to accomplish. in the header... typedef struct XXX_ELEMENT_tag { UINT8 num; UINT8 param; UINT16 fre1; INT16 a1;
0
9750
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,...
0
11023
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10740
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
9509
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...
1
7909
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7076
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
5936
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4550
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
4147
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.