Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 10-16-09   #1 (permalink)
Programmer
 
kartik3vv's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: Mysore , India
Posts: 523

Rep: 28 kartik3vv is acknowledged by some
Unique Rep: 24
Trader Rating: 0
Default Simple 'C' increment puzzle : Only Pros answer

hi all , just look at this code :

Quote:
#include<stdio.h>
int main() {
int i=6;
printf("\n%d\t%d\t%d\n",i++,i,++i);
return 0;

}
The output can be either : 6 7 8 or 7 7 7 (depending on compiler)
but the actual output is : 7 8 8

can anyone reason this ?
also the gimpel code that GCC generates is as follows :

Quote:
main ()
{
int i.0;
const char * restrict D.2047;
int D.2048;
int i;

i = 6;
i = i + 1;
i.0 = i;
i = i + 1;
D.2047 = (const char * restrict) "\n%d\t%d\t%d\n";
printf (D.2047, i.0, i, i);
D.2048 = 0;
return D.2048;
}
__________________

Get more help here , My Tech Blog

Kartik @ Exceeding Expectations

3DMak06 @ 11148
http://service.futuremark.com/result...&resultType=14

Core2Duo(E6600) @ 3.5 Ghz
http://valid.x86-secret.com/show_oc.php?id=381299

System: THE PRAETORIAN 731
CPU
Core2Duo E6600 @ 3.3Ghz(1.475Vcore)
Motherboard
ASUS P5N32-E SLI rev A2
Memory
1X4 GB Transcend @ OC 715 Mhz
Graphics Card
NVIDIA 8800 GTS @ 621/1404/975
Hard Drive
160x2 GB 'raid 0' Western Digital + 320 GB
Sound Card
Supreme Fx HD Audio
Power Supply
TX750 Corsair 750 Watts
Case
CoolerMaster Praetorian 731
CPU cooling
Coolermaster
GPU cooling
Stock
OS
Ubuntu 9.04 i686
Monitor
LG LCD 19" W1952S
kartik3vv is offline   Reply With Quote
Old 10-16-09   #2 (permalink)
Programmer
 
kartik3vv's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: Mysore , India
Posts: 523

Rep: 28 kartik3vv is acknowledged by some
Unique Rep: 24
Trader Rating: 0
Default

tough question :P
__________________

Get more help here , My Tech Blog

Kartik @ Exceeding Expectations

3DMak06 @ 11148
http://service.futuremark.com/result...&resultType=14

Core2Duo(E6600) @ 3.5 Ghz
http://valid.x86-secret.com/show_oc.php?id=381299

System: THE PRAETORIAN 731
CPU
Core2Duo E6600 @ 3.3Ghz(1.475Vcore)
Motherboard
ASUS P5N32-E SLI rev A2
Memory
1X4 GB Transcend @ OC 715 Mhz
Graphics Card
NVIDIA 8800 GTS @ 621/1404/975
Hard Drive
160x2 GB 'raid 0' Western Digital + 320 GB
Sound Card
Supreme Fx HD Audio
Power Supply
TX750 Corsair 750 Watts
Case
CoolerMaster Praetorian 731
CPU cooling
Coolermaster
GPU cooling
Stock
OS
Ubuntu 9.04 i686
Monitor
LG LCD 19" W1952S
kartik3vv is offline   Reply With Quote
Old 10-16-09   #3 (permalink)
Programmer
 
kartik3vv's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: Mysore , India
Posts: 523

Rep: 28 kartik3vv is acknowledged by some
Unique Rep: 24
Trader Rating: 0
Default

no one to answer ?
__________________

Get more help here , My Tech Blog

Kartik @ Exceeding Expectations

3DMak06 @ 11148
http://service.futuremark.com/result...&resultType=14

Core2Duo(E6600) @ 3.5 Ghz
http://valid.x86-secret.com/show_oc.php?id=381299

System: THE PRAETORIAN 731
CPU
Core2Duo E6600 @ 3.3Ghz(1.475Vcore)
Motherboard
ASUS P5N32-E SLI rev A2
Memory
1X4 GB Transcend @ OC 715 Mhz
Graphics Card
NVIDIA 8800 GTS @ 621/1404/975
Hard Drive
160x2 GB 'raid 0' Western Digital + 320 GB
Sound Card
Supreme Fx HD Audio
Power Supply
TX750 Corsair 750 Watts
Case
CoolerMaster Praetorian 731
CPU cooling
Coolermaster
GPU cooling
Stock
OS
Ubuntu 9.04 i686
Monitor
LG LCD 19" W1952S
kartik3vv is offline   Reply With Quote
Old 10-17-09   #4 (permalink)
4.0 GHz
 
dharmaBum's Avatar
 
intel nvidia

Join Date: Apr 2007
Location: Raleigh, NC
Posts: 747

Rep: 121 dharmaBum is acknowledged by manydharmaBum is acknowledged by many
Unique Rep: 89
Trader Rating: 0
Default

don't use printf?
__________________
3DMark06: 19091 - 3DMark Vantage: P15264 - SuperPi: 10.968s

Programming Quote of the Day:
Bjarne Stroustrup:
Quote:
There are only two industries that refer to their customers as ‘users’.

System: Europa
CPU
E8500 4.36ghz @ 1.36v
Motherboard
EVGA 780i SLi P05 Bios
Memory
G.SKILL 4GB(2x2GB) @ 924MHz (5-4-4-12-2T)
Graphics Card
2xEVGA 8800GTS (G92) 512MB @800/2000/2110
Hard Drive
Seagate 500gbx2, (fake-)RAID0
Sound Card
Sound Blaster X-Fi XtremeGamer
Power Supply
CORSAIR 1000HX 1000W
Case
Gigabyte GZ-FA2CA-AJB Black Aluminum
CPU cooling
TDX 775 Block, 360 BlackIce rad
GPU cooling
MAZE5x2, TT copper HS
OS
Fedora10-86_64/Vista64
Monitor
22" Samsung SyncMaster 2232BW
dharmaBum is offline   Reply With Quote
Old 10-17-09   #5 (permalink)
Programmer
 
kartik3vv's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: Mysore , India
Posts: 523

Rep: 28 kartik3vv is acknowledged by some
Unique Rep: 24
Trader Rating: 0
Default

Quote:
Originally Posted by dharmaBum View Post
don't use printf?
y not , i just wanted yo analyze this piece of code , before printf is called and gets a stack frame , its actual parameters must be evaluated i.e i i++ , ++i . thats what the compiler is doing but cant get to the result
__________________

Get more help here , My Tech Blog

Kartik @ Exceeding Expectations

3DMak06 @ 11148
http://service.futuremark.com/result...&resultType=14

Core2Duo(E6600) @ 3.5 Ghz
http://valid.x86-secret.com/show_oc.php?id=381299

System: THE PRAETORIAN 731
CPU
Core2Duo E6600 @ 3.3Ghz(1.475Vcore)
Motherboard
ASUS P5N32-E SLI rev A2
Memory
1X4 GB Transcend @ OC 715 Mhz
Graphics Card
NVIDIA 8800 GTS @ 621/1404/975
Hard Drive
160x2 GB 'raid 0' Western Digital + 320 GB
Sound Card
Supreme Fx HD Audio
Power Supply
TX750 Corsair 750 Watts
Case
CoolerMaster Praetorian 731
CPU cooling
Coolermaster
GPU cooling
Stock
OS
Ubuntu 9.04 i686
Monitor
LG LCD 19" W1952S
kartik3vv is offline   Reply With Quote
Old 10-17-09   #6 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

Simple answer, the evaluation order of function parameters is not defined by the standard.
__________________
Rich
Custom Wooden Case Builder
Overclock.net Mod of the Month
Spotswood is offline   Reply With Quote
Old 10-17-09   #7 (permalink)
Programmer
 
kartik3vv's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: Mysore , India
Posts: 523

Rep: 28 kartik3vv is acknowledged by some
Unique Rep: 24
Trader Rating: 0
Default

Quote:
Originally Posted by Spotswood View Post
Simple answer, the evaluation order of function parameters is not defined by the standard.
i thought it was either from right to left or from left to right , correct me if i'm wrong
__________________

Get more help here , My Tech Blog

Kartik @ Exceeding Expectations

3DMak06 @ 11148
http://service.futuremark.com/result...&resultType=14

Core2Duo(E6600) @ 3.5 Ghz
http://valid.x86-secret.com/show_oc.php?id=381299

System: THE PRAETORIAN 731
CPU
Core2Duo E6600 @ 3.3Ghz(1.475Vcore)
Motherboard
ASUS P5N32-E SLI rev A2
Memory
1X4 GB Transcend @ OC 715 Mhz
Graphics Card
NVIDIA 8800 GTS @ 621/1404/975
Hard Drive
160x2 GB 'raid 0' Western Digital + 320 GB
Sound Card
Supreme Fx HD Audio
Power Supply
TX750 Corsair 750 Watts
Case
CoolerMaster Praetorian 731
CPU cooling
Coolermaster
GPU cooling
Stock
OS
Ubuntu 9.04 i686
Monitor
LG LCD 19" W1952S
kartik3vv is offline   Reply With Quote
Old 10-17-09   #8 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

Quote:
Originally Posted by kartik3vv View Post
i thought it was either from right to left or from left to right , correct me if i'm wrong
Usually when someone says "right to left or from left to right" they are referring to the order of how parameters are pushed on the stack. That's not what I'm saying. Clearly (as shown by the gimpel code) the compiler made its own choice as to how to evaluate all of the increment operators before calling printf.
__________________
Rich
Custom Wooden Case Builder
Overclock.net Mod of the Month
Spotswood is offline   Reply With Quote
Old 10-17-09   #9 (permalink)
Programmer
 
kartik3vv's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: Mysore , India
Posts: 523

Rep: 28 kartik3vv is acknowledged by some
Unique Rep: 24
Trader Rating: 0
Default

Quote:
Originally Posted by Spotswood View Post
Usually when someone says "right to left or from left to right" they are referring to the order of how parameters are pushed on the stack. That's not what I'm saying. Clearly (as shown by the gimpel code) the compiler made its own choice as to how to evaluate all of the increment operators before calling printf.
i could find this info :



The C and C++ standards defines the order in which expressions in a C/C++ program are evaluated in terms of sequence points, which represent a partial ordering between the execution of parts of the program: those executed before the sequence point, and those executed after it. These occur after the evaluation of a full expression (one which is not part of a larger expression), after the evaluation of the first operand of a &&, ||, ? : or , (comma) operator, before a function is called (but after the evaluation of its arguments and the expression denoting the called function), and in certain other places. Other than as expressed by the sequence point rules, the order of evaluation of subexpressions of an expression is not specified. All these rules describe only a partial order rather than a total order, since, for example, if two functions are called within one expression with no sequence point between them, the order in which the functions are called is not specified. However, the standards committee have ruled that function calls do not overlap.

It is not specified when between sequence points modifications to the values of objects take effect. Programs whose behavior depends on this have undefined behavior; the C and C++ standards specify that “Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.”. If a program breaks these rules, the results on any particular implementation are entirely unpredictable.

Examples of code with undefined behavior are a = a++;, a[n] = b[n++] and a[i++] = i;. Some more complicated cases are not diagnosed by this option, and it may give an occasional false positive result, but in general it has been found fairly effective at detecting this sort of problem in programs.

The standard is worded confusingly, therefore there is some debate over the precise meaning of the sequence point rules in subtle cases. Links to discussions of the problem, including proposed formal definitions, may be found on the GCC readings page, at http://gcc.gnu.org/readings.html

the only thing i understood was : its undefined behavious / depends on compiler


Thanks a lot rep +
__________________

Get more help here , My Tech Blog

Kartik @ Exceeding Expectations

3DMak06 @ 11148
http://service.futuremark.com/result...&resultType=14

Core2Duo(E6600) @ 3.5 Ghz
http://valid.x86-secret.com/show_oc.php?id=381299

System: THE PRAETORIAN 731
CPU
Core2Duo E6600 @ 3.3Ghz(1.475Vcore)
Motherboard
ASUS P5N32-E SLI rev A2
Memory
1X4 GB Transcend @ OC 715 Mhz
Graphics Card
NVIDIA 8800 GTS @ 621/1404/975
Hard Drive
160x2 GB 'raid 0' Western Digital + 320 GB
Sound Card
Supreme Fx HD Audio
Power Supply
TX750 Corsair 750 Watts
Case
CoolerMaster Praetorian 731
CPU cooling
Coolermaster
GPU cooling
Stock
OS
Ubuntu 9.04 i686
Monitor
LG LCD 19" W1952S
kartik3vv is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 06:26 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.13705 seconds with 8 queries