|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Simple 'C' increment puzzle : Only Pros answer
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||||
|
Programmer
![]() |
hi all , just look at this code :
Quote:
but the actual output is : 7 8 8 can anyone reason this ? also the gimpel code that GCC generates is as follows : Quote:
__________________
![]() 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
|
|||||||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
Programmer
![]() |
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
|
|||||||||||||
|
|
|
|
|
#3 (permalink) | |||||||||||||
|
Programmer
![]() |
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
|
|||||||||||||
|
|
|
|
|
#4 (permalink) | ||||||||||||||
|
4.0 GHz
![]() |
don't use printf?
__________________
3DMark06: 19091 - 3DMark Vantage: P15264 - SuperPi: 10.968s Programming Quote of the Day: Bjarne Stroustrup: Quote:
|
||||||||||||||
|
|
|
|
|
#5 (permalink) | |||||||||||||
|
Programmer
![]() |
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
|
|||||||||||||
|
|
|
|
|
#7 (permalink) | |||||||||||||
|
Programmer
![]() |
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
|
|||||||||||||
|
|
|
|
|
#8 (permalink) |
|
Case Modder
![]() |
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
|
|
|
|
|
|
#9 (permalink) | ||||||||||||||
|
Programmer
![]() |
Quote:
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
|
||||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|