LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-28-2012, 04:11 AM   #1
ankitm
Member
 
Registered: Sep 2011
Posts: 33

Rep: Reputation: Disabled
ouput of the program varies with gcc version


Hi

Below is my sample code :

<code/>

int main(int argc,char *argv[])
{
int i = 1 ;
printf("%d%d%d", i++,--i,i--);
return 0;
}

</code>

When I complied this code with version gcc version 3.4.6 output was -1 -1 1 (expected) as c++ comuptes from left to right.

However when I complied it with gcc version 4.6.2 . output was -1 0 1.

Please explain this beahviour .

Thanks
 
Old 11-28-2012, 05:07 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by ankitm View Post
c++ comuptes from left to right.
No, it does not. The order of evaluation depends on operator precedence and associativity. So, for example,
Code:
1 + 2 + 3 + 4;
cout << a << b << c << d;
will be evaluated from left to right, but

Code:
a = b = c = d = 1;
      a += b += c;
will be evaluated from right to left.

Quote:
Originally Posted by ankitm View Post
However when I complied it with gcc version 4.6.2 . output was -1 0 1.
Thanks
You should also note that the C(++) standard does not specify order in which the function arguments will be evaluated, so this may very well depend on compiler and compile options such as optimization.

You may also examine the assembly code of your program to see what exactly happens there.

Edit: Also, read this

Last edited by millgates; 11-28-2012 at 05:17 AM.
 
Old 11-28-2012, 09:09 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
It is the order of evaluation of function arguments that is germane here. In this case, the ',' (comma) is not an operator, but simply a token separator. The C language says that the order of evaluation of function arguments is compiler dependent, therefore undefined.
This is a classic example of why you should not use such a construct.

--- rod.
 
1 members found this post helpful.
Old 11-28-2012, 10:14 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by ankitm View Post
printf("%d%d%d", i++,--i,i--);
This is not just an order of operations issue.

C++ does not specify which sequence the parameters are computed in, so any change in the way you compile could change that sequence (optimization level, compiler version, platform, phase of the moon).

But the problem is more serious than that. Multiple changes to the same variable in one expression is expressly undefined behavior. You not only have no reasonable expection of what sequence i++, --i and i-- will occur in, you have no reasonable expectation that they will all even occur. The value of i when this expression completes is unpredictable.

If this were just an order of evaluation issue, the three values passed to printf would be unpredictable within a very narrow range, while the resulting value of i would be reliably reduced by a net 1. In actual C++ the values passed are unpredictable across a wider range and the resulting value of i is also unpredictable.

The standard goes even further in specifying that use of an undefined construct make the entire behavior of the whole program undefined. You can't rely on what three values get passed to printf. You can't rely on what value ends up in i. You also can't rely on anything else done by any part of your program. In my opinion, the first two of those are very real concerns in a case like this and the third is silly. But the standard says all three should be concerns.

Last edited by johnsfine; 11-28-2012 at 10:23 AM.
 
1 members found this post helpful.
Old 12-02-2012, 07:06 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
It is silly and wrong.
Concentrate on good practice - not bad.
Life is too short.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Whats the difference with gcc 4.5.2-8ubuntu and gcc 4.5.2 generic version Arcosanti Ubuntu 4 08-19-2012 11:05 PM
[SOLVED] Upgraded GCC on Gentoo, different GCC version used when not root. fakie_flip Linux - Software 2 07-14-2012 11:11 AM
Regarding distribution + kernel version + gcc version + glib version. JCipriani Linux - General 8 04-19-2008 02:54 PM
redhat fedora gcc version (compiled for 2.4.20) doesn't match kernel version 2.4.22 start1000 Linux - Software 0 03-16-2004 08:17 PM
export CC=/usr/bin/gcc-3.2 - switch gcc version? ferreter Linux - Software 1 08-20-2003 12:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration