473,770 Members | 1,994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

iD8DBQFDcdVPRS5 AkKgtcCcRApTaAJ 4xOmWHjBooT+TB4 cmTjS7HB5EHAwCf eA0Q
N3wXlSuyQhDGsb9 hrWDU4f4=
=+COH
-----END PGP SIGNATURE-----
Nov 9 '05 #1
4 1924

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*******@gmai l.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.hc ltech.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*******@gmai l.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

iD8DBQFDcfmyRS5 AkKgtcCcRAkvgAJ 0a4AgEtrUVD09/77Qq8g17wzpA3wC fc2px
zIW7e9zLqEgDZFW wXaM06DQ=
=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
7182
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 pragmatic - 3) I usually move forward when I get the gut feeling I am correct - 4) Most likely because of 1), I usually do not manage to fully explain 3) when it comes true. - 5) I have developed for many years (>18) in many different environments,...
7
12967
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 needs to access the class defined in my second file and I can't figure out how to work this right. if I use an #include in my third file, my Main gives me a compile-time class redefinition error. if I don't, the third file can't "see" the second
82
12646
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 no Win32 specials allowed....
62
3795
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
5097
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 identifying memory leaks. The application in question is the Mozilla Web Browser. I also have had similar tasks before in the compiler construction area. And it is easy to come up with many more examples, where such kind of statistics can be very...
9
2700
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 and run it once you've made sure there is no harmful code. Currently we have several stored procedures which final result is a select with several joins that returns many
2
2432
by: dolphin | last post by:
What is the different between c++ call convention and c call convention?Can some give some examples?
6
1943
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 implementations gives two different results: 1) In the first implementation I compute the entire equation in memory 2) In the second implementation I store intermediate to variables, and then load them to compute the final result.
87
3750
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, b =3 lets say a sum operation is to be performed, then: output: 5
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10259
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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 we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.