Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Pre vs Post Incrementing variables

by cdarke (Prior)
on Sep 12, 2010 at 11:14 UTC ( [id://859830]=note: print w/replies, xml ) Need Help??


in reply to Pre vs Post Incrementing variables

a similar test program in a C++ IDE netted the exact same result.

In my experience, C and C++ compilers differ. Take the code:
int x = 0; printf("%d %d %d\n",++x,++x,++x);
A similar line in Perl (allowing for syntax differences) gives 3 3 3, because of optimisation.
PHP give 1 2 3, and probably denotes a lack of same.
MS Visual Studio 6 C++ you get 3 3 3 when compliled in Release (again, optimisation) but (wait for it) 3 2 1 in Debug (no optimisation).
gcc (Windows and Linux) gives 1 2 3 regardless of the optimisation level.

Upshot of that? Don't do it!

Replies are listed 'Best First'.
Re^2: Pre vs Post Incrementing variables
by SavannahLion (Pilgrim) on Sep 12, 2010 at 14:19 UTC

    Well... In the samples I gave above, the C++ IDE I used was MS Visual Studio 8 (or 2005, whatever it wants to call itself now) which gave 2 2 (or 3 3 3 in your sample) in the default Debug mode. So I guess virtually add it to the list there.

    It is, however, useful to know this about the compilers. I know the C family compilers have their own idiosyncrasies, I guess I'll put this on the pile things to note.

      I have noticed differences in later .Net versions of Visual Studio. I did not have them to hand when I wrote the node, and didn't want to guess. It could be that there is optimisation even in Debug. Check the project settings?

      So far as I can tell though, the order of execution (in C) of parameters is not defined. The reason why VS 6.0 (Debug) executed them from right to left was because that is the order of the C calling convention, __cdecl.
        cdecl controls the order in which the arguments are placed on the stack, not the order in which they are evaluated. While it tends to suggest a certain evaluation order, one cannot rely on it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://859830]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2025-07-13 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.