Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 why do I get the result like that
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
CHINA_yanglei




PostPosted: Thu Mar 26, 2009 11:08 pm   Post subject: why do I get the result like that

c++:
#include <iostream>
int main()
{
   int a=1,sum=0;
   std::cout<<sum;
   return 0;
}


the result is 10,why?
Thank you in advance!

Mod Edit: Remember to use syntax tags! Thanks Smile
code:
[syntax="cpp"]Code Here[/syntax]
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Mar 26, 2009 11:30 pm   Post subject: RE:why do I get the result like that

I don't get that output.
CHINA_yanglei




PostPosted: Fri Mar 27, 2009 10:55 am   Post subject: Re: why do I get the result like that

CHINA_yanglei @ 26/3/2009 wrote:
#include <iostream>
int main()
{
int a=1,sum=0;
sum=(++a)+(++a)+(++a);
std::cout<<sum;
return 0;
}
the result is 10,why?
Thank you in advance!
saltpro15




PostPosted: Sat Mar 28, 2009 3:32 pm   Post subject: RE:why do I get the result like that

I don't get 10 either, try running it again, I've seen this happen before where C++ likes to add 0's
A.J




PostPosted: Sat Mar 28, 2009 4:07 pm   Post subject: RE:why do I get the result like that

maybe you are running the wrong file..try compiling this file again, then try it
DanielG




PostPosted: Sat Mar 28, 2009 5:41 pm   Post subject: RE:why do I get the result like that

try outputting an endl after the 1, maybe it's just printing the 0 at the return 0 statement.
DemonWasp




PostPosted: Sat Mar 28, 2009 8:28 pm   Post subject: RE:why do I get the result like that

Weird. The following:

code:
#include <iostream>

int main() {
    int a = 1, sum = 0;
    sum += (++a);
    sum += (++a);
    sum += (++a);
    std::cout << sum << std::endl;
    return 0;
}


Produces the expected output of 9. The posted code produces the output of 10. Java also does the exact same thing correctly:

code:

public class test {
        public static void main ( String[] args ) {
                int a = 1, sum = 0;
                sum = (++a)+(++a)+(++a);
                System.out.println ( "sum = "+sum );
                System.out.println ( "a = "+a );
        }
}


The slightly simpler version produces similarly-incorrect output too:

code:
#include <iostream>

int main() {
    int a = 1, sum = 0;
    sum = (++a)+(++a)+(++a);
    std::cout << sum << std::endl;
    return 0;
}


Gives the result 6, when it should give 5 (Java gives the correct answer again).

Here's my theory:

The parse tree for the simple case looks like this:
code:
  root
  /  \
++a  ++a


Which would make it seem like it's evaluating its two children, then performing the sum. So it does ++a (a is now 2), then ++a (a is now 3), then it does a+a = 6.

In the more complex case, we have this parse tree:
code:
  root
  /  \
++a  expr
     /  \
    ++a ++a


It seems to be that in this case, some kind of temporary is used rather than the direct value of a. That is, at some point we take the value of a in place of a itself. So we'd have something like:

++a on the middle root, giving a = 2.
++a on the rightmost root, giving a = 3.
The value of the expr node is calculated to be (a)+(a) = 6, which is stored in a temporary value -> t = 6.
++a on the leftmost root, giving a = 4.
The total value is calculated as (a)+t = 4 + 6 = 10.

So the issue appears to be a temporary value from the result of a previous calculation being stored in a register, as well as out-of-order execution (both not uncommon; out-of-order execution is often done to require fewer temporary values). This is borne out by expanding the problem further:

code:

#include <iostream>

int main () {
        int a = 1, sum = 0;

        sum = (++a)+(++a)+(++a)+(++a);

        std::cout << "sum = " << sum << std::endl;

        return 0;
}


Should give:

code:
  root
  /  \
++a  expr
     /  \
    ++a expr
        /  \
       ++a ++a


Based on the expression, we would expect sum = 2+3+4+5 = 14
Based on my explanation, we should expect sum = 5 + ( 4 + ( 3 + 3 ) ) = 15

We get 15.

So my conclusion is that this is a compiler bug, or else a problem with the specifications. The simple answer is to not use preincrement on the same variable multiple times on the same line.
DemonWasp




PostPosted: Sat Mar 28, 2009 8:33 pm   Post subject: RE:why do I get the result like that

Note: I did my tests using both gcc and g++, which exhibit the same issue, and the SUN Java compiler / JVM, which does not. Different compilers may behave differently - if everyone could list which compiler they're using, that might be helpful.
Sponsor
Sponsor
Sponsor
sponsor
sharvil




PostPosted: Sat Apr 04, 2009 11:48 am   Post subject: Re: why do I get the result like that

The order of evaluation of the + operator is implementation specific (in C and C++) so the compiler is free to choose any order. As DemonWasp pointed out, there's an evaluation order that can produce 10 so the output is correct.

The real issue is that your expression has subexpressions (++a) that have side-effects on shared state (a). The way to resolve it is by adding explicit sequence points (as DemonWasp did in the sum += (++a) example).
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: