On 2011-07-28 15.22, Gene Buckle wrote: > On Thu, 28 Jul 2011, Jari Häkkinen wrote: > >>>> Are you sure about that? I just tried it with a little example and >>>> at least >>>> gcc compiles both variants to the exact same assembly code. Tried it >>>> with and >>>> without -O2. >>>> >>> That would freak me out. Doesn't "++j" mean "increment j, then test" >>> whereas "j++" means "test j, then increment"? >> >> No, for a for loop >> >> for ( [1]; [2]; [3] ) >> >> where [3] is ++j will increment j before use. However, in an >> if-statement the complete statement [3] is evaluated before the test [2] >> is done. If the compiler is smart it will produce the fastest binary >> code regardless ++j or j++. However, if the [3] is more complicated like >> a hypothetical i = ++j + k the compiler will most probably generate >> different binary code (compared to i = ++j + k). > > Right, but j++ will increment _after_ it's used, correct? So how could > ++j vs j++ generate the same assembly code and be correct?
Yes, j++ will increment j after use. The same assembly code in this case is probably because gcc makes optimizations for simple statements like j++; or ++j; These are straight forward for the compiler to perform optimizations for (at least for standard types like doubles, integers, chars etc). For general classes it is probably not legal for the compiler to make similar assumptions when optimizing since the operators ++C and C++ can in principle behave completely differently/unexpectedly ... we are in the mercy of the programmer here. Jari ------------------------------------------------------------------------------ Got Input? Slashdot Needs You. Take our quick survey online. Come on, we don't ask for help often. Plus, you'll get a chance to win $100 to spend on ThinkGeek. http://p.sf.net/sfu/slashdot-survey _______________________________________________ Flightgear-devel mailing list Flightgear-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/flightgear-devel