473,383 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

-----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 [i]
004113C8 add eax,1
004113CB mov dword ptr [i],eax
004113CE mov ecx,dword ptr [i]
004113D1 add ecx,1
004113D4 mov dword ptr [i],ecx
004113D7 mov edx,dword ptr [i]
004113DA add edx,1
004113DD mov dword ptr [i],edx
004113E0 mov eax,dword ptr [i]
004113E3 add eax,dword ptr [i]
004113E6 add eax,dword ptr [i]
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+TB4cmTjS 7HB5EHAwCfeA0Q
N3wXlSuyQhDGsb9hrWDU4f4=
=+COH
-----END PGP SIGNATURE-----
Nov 9 '05 #1
4 1904

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-lit...html#faq-39.15

<snip>

Gavin Deane

Nov 9 '05 #2

"Tao Wang" <da*******@gmail.com> wrote in message
news:lz******************@news-server.bigpond.net.au...
-----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-lit...html#faq-39.15
http://www.parashift.com/c++-faq-lit...html#faq-39.16

http://www.angelikalanger.com/Articl...ncePoints.html
Regards,
Sumit.
--
Sumit Rajan <su****@msdc.hcltech.com>
Nov 9 '05 #3
Geo

Tao Wang wrote:
int c = (++i)+(++i)+(++i);


This is undefined behaviour, the rest of the program is irrelevant,
anything could happen.

Nov 9 '05 #4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sumit Rajan wrote:
"Tao Wang" <da*******@gmail.com> wrote in message
news:lz******************@news-server.bigpond.net.au...
-----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-lit...html#faq-39.15
http://www.parashift.com/c++-faq-lit...html#faq-39.16

http://www.angelikalanger.com/Articl...ncePoints.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-----
Nov 9 '05 #5

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

Similar topics

137
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very...
7
by: A_StClaire_ | last post by:
hi, I'm working on a project spanning five .cpp files. each file was used to define a class. the first has my Main and an #include for each of the other files. problem is my third file...
82
by: zardoz | last post by:
I've got this problem: unsigned long long lTemp; char cLargeNum="1324567890"; sscanf(clargeNum,"%llu",&lTemp); which under Win32 isn't working*. My program needs to compile under posix so...
62
by: ashu | last post by:
hi look at this code include <stdio.h> int main(void) { int i,j=2; i=j++ * ++j * j++; printf("%d %d",i,j); return 0;
17
by: romixnews | last post by:
Hi, I'm facing the problem of analyzing a memory allocation dynamic and object creation dynamics of a very big C++ application with a goal of optimizing its performance and eventually also...
9
by: serge | last post by:
/* Subject: How to build a procedure that returns different numbers of columns as a result based on a parameter. You can copy/paste this whole post in SQL Query Analyzer or Management Studio...
2
by: dolphin | last post by:
What is the different between c++ call convention and c call convention?Can some give some examples?
6
by: Avi | last post by:
I need to implement the following calculation: f = (a*b) + (c*d) where a,b,c,d are given double values and f is a double variable for the result I found out that using two different...
87
by: pereges | last post by:
I have a C program which I created on Windows machine. I have compiled and executed the program on windows machine and it gives me the consistent output every time i run it. for eg. input a = 2,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.