Click to See Complete Forum and Search --> : ++x + ++x
Omarkth
March 31st, 2006, 03:54 PM
Hi, This is my first post, and hope not to be the last.
Any one can tell me the result of this code, and the most importent: Why??
the code is:
=======================
int x = 3;
cout << ++x + ++x <<endl;
cout << x;
=======================
please test it before replying
YourSurrogateGod
March 31st, 2006, 04:14 PM
Is this a homework assignment?
Omarkth
March 31st, 2006, 04:19 PM
Is this a homework assignment?
:) No its NOT!!
I was studing for the next week exam, and this problem showed up.
if you know any thing about it.. i'll be glad to hear it!!
exterminator
March 31st, 2006, 04:19 PM
The code does not compile. Okay, no kidding ;)
Undefined behaviour... why? Because the C++ standard says so.
Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. 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 sub-expressions of a full-expression; otherwise the behavior is undefined.
[Example:
i = v[i++]; // the behavior is undefined
i = 7, i++, i++; // ‘i’ becomes 9
i = ++i + 1; // the behavior is undefined
i = i + 1; // the value of ’i’ is incremented—end example]Hope this helps. Regards.
humptydumpty
March 31st, 2006, 04:24 PM
just check out in MSDN for Operator Precedence and Associativity and see how it will work.
int main()
{
int x=1;
cout << ++x + ++x <<endl; //produce output 6
return 0;
//to run this program i used VC6.0 and it's perfectly valid
}
Thankyou
exterminator
March 31st, 2006, 04:28 PM
Some relevant readings:
1. What is the output of this program (http://www.codeguru.com/forum/showthread.php?t=346166&highlight=sequence+point)
2. undefined behaviour (http://www.codeguru.com/forum/showthread.php?t=353182&highlight=undefined+behaviour)
Regards.
Omarkth
March 31st, 2006, 04:32 PM
Exterminator:
why doesn't it compile? undefined behavior!!
it did compile with me, but the result weren't as expected
humptydumpty:
I checked there and i know that it has some thing to do with the operators precedency... but there are nothing about such case.
All:
I'll be more specific:
preincrement operator (++) has precedenc over the addition operator (+) but it compile from right to left,
So, when we say :
++x + ++x
this should be compiled and interpruted as:
5 + 4
then the result is:
9
but when you test it with C++ compiler : the result is 10
this is the problem... and this is what i know.
If i'm mistaken,, please correct me!
YourSurrogateGod
March 31st, 2006, 04:38 PM
it did compile with me, but the result weren't as expected
What compiler are you using and what were the unexpected results?
MrViggy
March 31st, 2006, 04:41 PM
preincrement operator (++) has precedenc over the addition operator (+) but it compile from right to left,
So, when we say :
++x + ++x
this should be compiled and interpruted as:
5 + 4
then the result is:
9
but when you test it with C++ compiler : the result is 10
this is the problem... and this is what i know.
No. If I remember correctly, pre/post increment operators involved in function calls are undefined as to when they will be executed. For example:
int h = 2;
f = g(++h) + i(++h);
If 'h' is in int, and 'g' and 'i' are functions, what value is passed to 'g' and what value is passed to 'i'?
I've run into bugs in code, for this exact problem. The code "worked" on one platform, but didn't "work" on another, because of the compiler's implementation of function calls and the pre-increment operator.
Viggy
Omarkth
March 31st, 2006, 04:46 PM
I'm using VC6.0....
Okey, MrViggy: can you explain why the result came out like that??
if you wrote:
x = 3;
cout << ++x << ++x;
OUTPUT: 54
but if you make it:
x=3;
cout << ++x + ++x;
OUTPUT: 10 // insted of 5+4=9 ???? WHY??
MrViggy
March 31st, 2006, 04:55 PM
5 + 5 == 10
In other words, the compiler did the pre-increments first, then the addition.
Viggy
Omarkth
March 31st, 2006, 04:58 PM
that's what i'm asking about :
why 5 + 5???
why not 5 + 4 ??
x originally was 3 : first increment made it 4, second made it 5 ===> must be 4 + 5 , not 5 + 5
u see what i'm talking about?
exterminator
March 31st, 2006, 05:14 PM
that's what i'm asking about :
why 5 + 5???
why not 5 + 4 ??
x originally was 3 : first increment made it 4, second made it 5 ===> must be 4 + 5 , not 5 + 5
u see what i'm talking about?Please re-read my post again. I was kidding when I said your code does not compile. In fact, it would not compile in the state you have provided it to us. But considering the fact that there is program that has such statement - the answer is what followed in the later lines (next to first one - the kidding one :D ).
It is an undefined behaviour. Before posting any more please atleast read the quote that i provided and if still un-certain read the threads, the link to which I provided in my next post. That statement is free to give any result on any compiler. Regards.
MrViggy
March 31st, 2006, 05:17 PM
Because the order of those operations is undefined (as was stated earlier), and compiler dependent! Take this example:
#include <iostream>
int SomeFuncA (int a)
{
std::cout << "In SomeFuncA...\n";
return a;
}
int SomeFuncB (int b)
{
std::cout << "In SomeFuncB...\n";
return b;
}
int main(/*int argc, char *argv[]*/)
{
int tmpVal = 3;
std::cout << "Starting...\n";
std::cout << SomeFuncA(++tmpVal) << SomeFuncB(++tmpVal) << std::endl;
std::cout << "Now, just test the pre-inc...\n";
tmpVal = 3;
std::cout << ++tmpVal << " " << ++tmpVal << std::endl;
return 0;
}
When I run this using Visual Studio 6, I get:
Starting...
In SomeFuncB...
In SomeFuncA...
54
Now, just test the pre-inc...
5 4
The same exact program on Visual Studio 7 outputs:
Starting...
In SomeFuncB...
In SomeFuncA...
55
Now, just test the pre-inc...
5 5
Two different compilers, two different results.
Viggy
Omarkth
March 31st, 2006, 05:21 PM
thank you MrViggy, this is what i was really waiting for..
thanx again
MrViggy
March 31st, 2006, 05:25 PM
No prob!
Vig.
INeedHelp2
March 31st, 2006, 09:14 PM
Viggy had it right.
Operator precedence would evaluate the ++x first, so x = 5 after this is done. Then x + x = 10
SuperKoko
April 1st, 2006, 01:08 AM
Operator precedence would evaluate the ++x first, so x = 5 after this is done. Then x + x = 10
It is only one possibility.
There are plenty others:
compile-time error.
crash
It is very probable that it may yield any value in range [7, 10]
And, saying simply that "side effects appear in any order" is not exact.
Side effects can appear in any order and may even be interlaced!
For example, if there were a primitive operator "swap bytes" (little endian <-> big endian), doing something like:
swapbytes(swapbytes(x))
Would probably make some bytes appear twice, and others not at all, and in any order...
Indeed any program that make two side effects on the same lvalue between two sequence points, has undefined behaviour.
It may play the 9th symphony of Beethoven...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.