why get different result of a simple code on different compiler?

Discussion in 'C++' started by Tao Wang, Nov 9, 2005.

  1. Tao Wang

    Tao Wang Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Hi,

    I am quite confused on a equation, as following:

    #include <iostream>
    int main(){
    int i = 2;
    int c = (++i)+(++i)+(++i);
    std::cout << c << std::endl;
    }

    In my mind, the final result should be 12, since 3+4+5. But the result I
    got is interesting.

    Visual C++ .Net 2005 (8.0) - C++: Result: 15
    Visual C++ .Net 2005 (8.0) - C++/CLI: Result: 15
    Visual C++ 6.0 Result: 13

    Borland C++ Builder 5 Result: 12

    gcc version 3.4.2 (mingw-special): Result: 13
    g++ (GCC) 4.0.1 20050727 (Red Hat 4.0.1-5) Result: 13

    Visual C# .Net 2005 - C# Result: 12
    Java version "1.5.0_04" Result: 12

    I tried to see the assembly of the executable file. I found they
    implemented quite different. Is there anyone can tell me why? Which one
    is correct? How the C++ standard explain it?

    The corresponding assembly code is following:

    Visual C++ .Net 2005 (8.0) - C++:
    Result: 15

    int c = (++i)+(++i)+(++i);

    004113C5 mov eax,dword ptr
    004113C8 add eax,1
    004113CB mov dword ptr ,eax
    004113CE mov ecx,dword ptr
    004113D1 add ecx,1
    004113D4 mov dword ptr ,ecx
    004113D7 mov edx,dword ptr
    004113DA add edx,1
    004113DD mov dword ptr ,edx
    004113E0 mov eax,dword ptr
    004113E3 add eax,dword ptr
    004113E6 add eax,dword ptr
    004113E9 mov dword ptr [c],eax


    Visual C++ .Net 2005 (8.0) - C++/CLI:
    Result: 15

    int c = (++i)+(++i)+(++i);

    00000022 inc esi
    00000023 inc esi
    00000024 inc esi
    00000025 lea eax,[esi+esi]
    00000028 add eax,esi
    0000002a mov ebx,eax

    gcc version 3.4.2 (mingw-special):
    Result: 13

    int c = (++i)+(++i)+(++i);

    40147f: 8d 45 e4 lea 0xffffffe4(%ebp),%eax
    401482: ff 00 incl (%eax)
    401484: 8d 45 e4 lea 0xffffffe4(%ebp),%eax
    401487: ff 00 incl (%eax)
    401489: 8b 45 e4 mov 0xffffffe4(%ebp),%eax
    40148c: 8b 55 e4 mov 0xffffffe4(%ebp),%edx
    40148f: 01 c2 add %eax,%edx
    401491: 8d 45 e4 lea 0xffffffe4(%ebp),%eax
    401494: ff 00 incl (%eax)
    401496: 89 d0 mov %edx,%eax
    401498: 03 45 e4 add 0xffffffe4(%ebp),%eax
    40149b: 89 45 e0 mov %eax,0xffffffe0(%ebp)

    g++ (GCC) 4.0.1 20050727 (Red Hat 4.0.1-5)
    Result: 13

    int c = (++i)+(++i)+(++i);

    leal -8(%ebp), %eax
    incl (%eax)
    leal -8(%ebp), %eax
    incl (%eax)
    movl -8(%ebp), %eax
    movl -8(%ebp), %edx
    addl %eax, %edx
    leal -8(%ebp), %eax
    incl (%eax)
    movl %edx, %eax
    addl -8(%ebp), %eax

    Visual C# .Net 2005 - C#
    Result: 12

    int c = (++i) + (++i) + (++i);

    0000002e inc esi
    0000002f mov ebx,esi
    00000031 inc esi
    00000032 add ebx,esi
    00000034 inc esi
    00000035 add ebx,esi
    00000037 mov edi,ebx

    Java version "1.5.0_04"
    Result: 12

    public static void main(java.lang.String[]);
    Code:
    0: iconst_2
    1: istore_1
    2: iinc 1, 1
    5: iload_1
    6: iinc 1, 1
    9: iload_1
    10: iadd
    11: iinc 1, 1
    14: iload_1
    15: iadd
    16: istore_2
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)
    Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

    iD8DBQFDcdVPRS5AkKgtcCcRApTaAJ4xOmWHjBooT+TB4cmTjS7HB5EHAwCfeA0Q
    N3wXlSuyQhDGsb9hrWDU4f4=
    =+COH
    -----END PGP SIGNATURE-----
    Tao Wang, Nov 9, 2005
    #1
    1. Advertising

  2. Tao Wang

    Guest

    Tao Wang wrote:
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > Hi,
    >
    > I am quite confused on a equation, as following:
    >
    > #include <iostream>
    > int main(){
    > int i = 2;
    > int c = (++i)+(++i)+(++i);
    > std::cout << c << std::endl;
    > }
    >
    > In my mind, the final result should be 12, since 3+4+5. But the result I
    > got is interesting.


    You got undefined behaviour. You are lucky that the result you saw was
    something other than what you expected.

    http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-39.15

    <snip>

    Gavin Deane
    , Nov 9, 2005
    #2
    1. Advertising

  3. Tao Wang

    Sumit Rajan Guest

    "Tao Wang" <> wrote in message
    news:lzkcf.12917$...
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > Hi,
    >
    > I am quite confused on a equation, as following:
    >
    > #include <iostream>
    > int main(){
    > int i = 2;
    > int c = (++i)+(++i)+(++i);
    > std::cout << c << std::endl;
    > }
    >
    > In my mind, the final result should be 12, since 3+4+5. But the result I
    > got is interesting.


    Please see:
    http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-39.15
    http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-39.16

    http://www.angelikalanger.com/Articles/VSJ/SequencePoints/SequencePoints.html


    Regards,
    Sumit.
    --
    Sumit Rajan <>
    Sumit Rajan, Nov 9, 2005
    #3
  4. Tao Wang

    Geo Guest

    Tao Wang wrote:

    > int c = (++i)+(++i)+(++i);


    This is undefined behaviour, the rest of the program is irrelevant,
    anything could happen.
    Geo, Nov 9, 2005
    #4
  5. Tao Wang

    Tao Wang Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Sumit Rajan wrote:
    > "Tao Wang" <> wrote in message
    > news:lzkcf.12917$...
    >
    >>-----BEGIN PGP SIGNED MESSAGE-----
    >>Hash: SHA1
    >>
    >>Hi,
    >>
    >>I am quite confused on a equation, as following:
    >>
    >>#include <iostream>
    >>int main(){
    >> int i = 2;
    >> int c = (++i)+(++i)+(++i);
    >> std::cout << c << std::endl;
    >>}
    >>
    >>In my mind, the final result should be 12, since 3+4+5. But the result I
    >>got is interesting.

    >
    >
    > Please see:
    > http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-39.15
    > http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-39.16
    >
    > http://www.angelikalanger.com/Articles/VSJ/SequencePoints/SequencePoints.html
    >
    >
    > Regards,
    > Sumit.

    Oh, I see, Thanks

    Dancefire
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)
    Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

    iD8DBQFDcfmyRS5AkKgtcCcRAkvgAJ0a4AgEtrUVD09/77Qq8g17wzpA3wCfc2px
    zIW7e9zLqEgDZFWwXaM06DQ=
    =DUvx
    -----END PGP SIGNATURE-----
    Tao Wang, Nov 9, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yzhshi
    Replies:
    7
    Views:
    481
    yzhshi
    Apr 16, 2004
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    838
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,667
    Smokey Grindel
    Dec 2, 2006
  4. Michael Tan
    Replies:
    32
    Views:
    908
    Ara.T.Howard
    Jul 21, 2005
  5. denmat
    Replies:
    2
    Views:
    161
    denmat
    Apr 23, 2008
Loading...

Share This Page