473,506 Members | 16,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unusual operator behavior

Greetings!
I'm wondering how do the expressions in the following piece of code
evaluate and why:

#include <iostream>
using namespace std;

int main()
{
int n = 5, p;
n = n + n++;
cout << "n = " << n << endl;
n = 5;
p = n + n++;
cout << "p = " << p << endl;

return 0;
}
I compiled it both with g++ and bcc32 and the results are identical:
n = 11
p = 10
The first result seems obvious. But I don't know what's going on with
the evaluation of the p variable. I am really interested on how it
works. Thank you in advance,
Karlo.

Jul 19 '05 #1
11 2248
Karlo Basic wrote:
I'm wondering how do the expressions in the following piece of code
evaluate and why:

#include <iostream>
using namespace std;

int main()
{
int n = 5, p;
n = n + n++;
This expression modifies the stored value of 'n' more than once.
Undefined behavior.
cout << "n = " << n << endl;
n = 5;
p = n + n++;
This expression modifies 'n' and at the same time reads its old value
for a purpose other than determining its new value. Undefined behavior.
cout << "p = " << p << endl;

return 0;
}
I compiled it both with g++ and bcc32 and the results are identical:
n = 11
p = 10
The first result seems obvious. But I don't know what's going on with
the evaluation of the p variable. I am really interested on how it
works.


There's no way to predict or explain how it will work. Both expressions
result in undefined behavior.

--
Best regards,
Andrey Tarasevich

Jul 19 '05 #2


Karlo Basic wrote:

Greetings!
I'm wondering how do the expressions in the following piece of code
evaluate and why:

#include <iostream>
using namespace std;

int main()
{
int n = 5, p;
n = n + n++;
modifying a variable twice between sequence points -> undefined behaviour
cout << "n = " << n << endl;
n = 5;
p = n + n++;
cout << "p = " << p << endl;

return 0;
}
I compiled it both with g++ and bcc32 and the results are identical:
n = 11
p = 10
The first result seems obvious.
Not really.
But I don't know what's going on with
the evaluation of the p variable. I am really interested on how it
works.


There is no correct answer. The compiler can do anything it wants.
Your program has produced undefined behaviour and it is pointless
to think about a 'correct solution' since there is none.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #3
The relevant part of the standard is Section 5 paragraph 4

Except where noted, the order of evaluation of operands of individual
operators and subexpressions of indi-vidual expressions, and the order
in which side effects take place, is unspecified.53) Between the
previous and next sequence point a scalar object shall have its stored
value modified at most once by the evaluation of an expression.
Furthermore, the prior value shall be accessed only to determine the
value to be stored. The requirements of this paragraph shall be met for
each allowable ordering of the subexpressions of a full expression;
otherwise the behavior is undefined. [Example:
i = v[i++]; // the behavior is unspecified
i = 7, i++, i++; // i becomes 9
i = ++i + 1; // the behavior is unspecified
i = i + 1; // the value of i is incremented
—end example]

Jul 19 '05 #4
A
I'm wondering how do the expressions in the following piece of code
evaluate and why:

#include <iostream>
using namespace std;

int main()
{
int n = 5, p;
n = n + n++;
cout << "n = " << n << endl;
n = 5;
p = n + n++;
cout << "p = " << p << endl;

return 0;
}
I compiled it both with g++ and bcc32 and the results are identical:
n = 11
p = 10
The first result seems obvious. But I don't know what's going on with
the evaluation of the p variable. I am really interested on how it
works. Thank you in advance,
Karlo.


n = 5;
p = n + n++; // EASY! add 5 and 5 and assign it to p and then increment n
(postfix increment)
cout << "p = " << p << endl;

Regards,
A
Jul 19 '05 #5


A wrote:
I'm wondering how do the expressions in the following piece of code
evaluate and why:

#include <iostream>
using namespace std;

int main()
{
int n = 5, p;
n = n + n++;
cout << "n = " << n << endl;
n = 5;
p = n + n++;
cout << "p = " << p << endl;

return 0;
}
I compiled it both with g++ and bcc32 and the results are identical:
n = 11
p = 10
The first result seems obvious. But I don't know what's going on with
the evaluation of the p variable. I am really interested on how it
works. Thank you in advance,
Karlo.


n = 5;
p = n + n++; // EASY! add 5 and 5 and assign it to p and then increment n
(postfix increment)


Not at all.
The compiler coud also do.

Take the value of n 5
increment n
Take the value of n 6
Add both numbers

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #6
Here is what is going on here:

n++ evaluates the expression first and add 1 to it. Therefore

n = n + n++ (= 5 + 5) = 10 after evaluation 1 is added to n therefore n =
11
p = n+n++ (= 5+5) = 10, n is = 6 after this expression but p is still 10.

In short,

++n increment the n first
n++ evaluate the expression first

Hope it explained your confusion.
Dev

"Karlo Basic" <ka*****@email.si> wrote in message
news:bo*************@ID-212926.news.uni-berlin.de...
Greetings!
I'm wondering how do the expressions in the following piece of code
evaluate and why:

#include <iostream>
using namespace std;

int main()
{
int n = 5, p;
n = n + n++;
cout << "n = " << n << endl;
n = 5;
p = n + n++;
cout << "p = " << p << endl;

return 0;
}
I compiled it both with g++ and bcc32 and the results are identical:
n = 11
p = 10
The first result seems obvious. But I don't know what's going on with
the evaluation of the p variable. I am really interested on how it
works. Thank you in advance,
Karlo.

Jul 19 '05 #7
"Sinora" <s_*********@hotmail.com> wrote...
Here is what is going on here:

n++ evaluates the expression first and add 1 to it. Therefore
Stop right here. Adding of 1 to 'n' does NOT have to happen at the
same time as the insertion of the value of 'n' into the expression.

As others have mentioned, the behaviour of this code is undefined.

n = n + n++ (= 5 + 5) = 10 after evaluation 1 is added to n therefore n =
11
p = n+n++ (= 5+5) = 10, n is = 6 after this expression but p is still 10.

In short,

++n increment the n first
n++ evaluate the expression first

Hope it explained your confusion.
You are confused no less than the original poster. Read the posts
of others.
Dev

"Karlo Basic" <ka*****@email.si> wrote in message
news:bo*************@ID-212926.news.uni-berlin.de...
Greetings!
I'm wondering how do the expressions in the following piece of code
evaluate and why:

#include <iostream>
using namespace std;

int main()
{
int n = 5, p;
n = n + n++;
cout << "n = " << n << endl;
n = 5;
p = n + n++;
cout << "p = " << p << endl;

return 0;
}
I compiled it both with g++ and bcc32 and the results are identical:
n = 11
p = 10
The first result seems obvious. But I don't know what's going on with
the evaluation of the p variable. I am really interested on how it
works. Thank you in advance,
Karlo.


Jul 19 '05 #8
"Sinora" <s_*********@hotmail.com> wrote...
Here is what is going on here:

n++ evaluates the expression first and add 1 to it. Therefore
Stop right here. Adding of 1 to 'n' does NOT have to happen at the
same time as the insertion of the value of 'n' into the expression.

As others have mentioned, the behaviour of this code is undefined.

n = n + n++ (= 5 + 5) = 10 after evaluation 1 is added to n therefore n =
11
p = n+n++ (= 5+5) = 10, n is = 6 after this expression but p is still 10.

In short,

++n increment the n first
n++ evaluate the expression first

Hope it explained your confusion.
You are confused no less than the original poster. Read the posts
of others.
Dev

"Karlo Basic" <ka*****@email.si> wrote in message
news:bo*************@ID-212926.news.uni-berlin.de...
Greetings!
I'm wondering how do the expressions in the following piece of code
evaluate and why:

#include <iostream>
using namespace std;

int main()
{
int n = 5, p;
n = n + n++;
cout << "n = " << n << endl;
n = 5;
p = n + n++;
cout << "p = " << p << endl;

return 0;
}
I compiled it both with g++ and bcc32 and the results are identical:
n = 11
p = 10
The first result seems obvious. But I don't know what's going on with
the evaluation of the p variable. I am really interested on how it
works. Thank you in advance,
Karlo.


Jul 19 '05 #9
Sinora wrote:
Here is what is going on here:

n++ evaluates the expression first and add 1 to it. Therefore

n = n + n++ (= 5 + 5) = 10 after evaluation 1 is added to n therefore n =
11
p = n+n++ (= 5+5) = 10, n is = 6 after this expression but p is still 10.

In short,

++n increment the n first
n++ evaluate the expression first

Hope it explained your confusion.


The explanation is incorrect and can only add to the confusion.

--
Best regards,
Andrey Tarasevich

Jul 19 '05 #10
"Sinora" <s_*********@hotmail.com> wrote in message
news:bo**********@news.iastate.edu...
Here is what is going on here:

n++ evaluates the expression first and add 1 to it. Therefore

n = n + n++ (= 5 + 5) = 10 after evaluation 1 is added to n therefore n =
11
p = n+n++ (= 5+5) = 10, n is = 6 after this expression but p is still 10.

In short,

++n increment the n first
n++ evaluate the expression first

Hope it explained your confusion.
Dev


Well I did look it up in the C++ standard and it really says that this
behaviour is undefined. So this explanation, although logical, doesn't quite
do the trick.
So I am asking again: why is it implemented this way? Are other programming
languages behaving the same way with these kind of expressions? Isn't this
undefined behaviour dangerous?
Thanks,
Karlo.
Jul 19 '05 #11


Karlo wrote:

"Sinora" <s_*********@hotmail.com> wrote in message
news:bo**********@news.iastate.edu...
Here is what is going on here:

n++ evaluates the expression first and add 1 to it. Therefore

n = n + n++ (= 5 + 5) = 10 after evaluation 1 is added to n therefore n =
11
p = n+n++ (= 5+5) = 10, n is = 6 after this expression but p is still 10.

In short,

++n increment the n first
n++ evaluate the expression first

Hope it explained your confusion.
Dev
Well I did look it up in the C++ standard and it really says that this
behaviour is undefined. So this explanation, although logical, doesn't quite
do the trick.
So I am asking again: why is it implemented this way?


To open up a door for the compiler for optimizations.
Are other programming
languages behaving the same way with these kind of expressions?
Don't know. Ask in a newgroup which discusses 'other programming languages'.
Isn't this
undefined behaviour dangerous?


In principle yes. In practice: no. It is very rare that one does the
above in real code. All those posted examples are academic and constructed
to show the 'undefined behaviour'. In 20 years I still am waiting to ever
write
n = n + n++;
or
p = n + n++;

And even if I find the need to do so, I would write:

n *= 2;
p = 2 * n++;

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #12

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

Similar topics

30
10379
by: | last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated) before. In general when I have a C++ question I look for answers in "The C++ Programming Language, Third Edition" by Stroustrup....
16
2565
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
3
1681
by: Farooq Khan | last post by:
why does Response.Write in a method of code-beind class when called from inpage code (i.e in <%---%>), after creating object of that class, fails when called while it works perfectly ok while...
2
11508
by: Shark | last post by:
Hi, if we need to change the behavior of operator new, it is called overriding or overloading? My other question is, if we change the behavior of operator new, do we use malloc to do that or we use...
7
2501
by: Edward Diener | last post by:
Since implement the assign operator for reference types eliminates the ability to assign a reference object to a reference variable of the same type or base class of that type, I assume that...
7
10155
by: brett.estabrook | last post by:
I have written a multi-threaded c# windows service in .net 1.1 (Visual Studio .net 2003). The service has several threads that poll a Sql 6.5 database on another machine. Each thread will execute a...
19
2479
by: Rajesh S R | last post by:
Consider the following code: int main() { struct name { long a; int b; long c; }s={3,4,5},*p; p=&s;
9
2046
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
30
2654
by: Angel Tsankov | last post by:
Hello! Does the C++ standard define what happens when the size argument of void* operator new(size_t size) cannot represent the total number of bytes to be allocated? For example: struct S {...
0
7218
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
7103
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
7307
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,...
0
7370
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
7021
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
7478
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
5035
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
1532
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
755
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.