|
Posted by Malcolm Dew-Jones on 04/12/07 16:47
David T. Ashley (dta@e3ft.com) wrote:
: "Andy Hassall" <andy@andyh.co.uk> wrote in message
: news:63nq13tlvg0fvp5i0lg54uvlo18nhi5n3f@4ax.com...
: > On Wed, 11 Apr 2007 16:20:45 -0500, "Steve" <no.one@example.com> wrote:
: >
: >>someone asked a question in alt.php about a problem they were having with
: >>an
: >>algorytm. it contained something to the effect of:
: >>
: >>$i = $i++;
: >>
: >>some example code they'd snatched somewhere. in a loop, the expected $i to
: >>increment. i explained why i thought it would not - as it does not.
: >>however,
: >>i want to make sure i gave a valid answer.
: >>
: >>anyone have input?
: >
: > In C and C++ that's undefined. PHP is less formally defined and so it is
: > even
: > more daft to try it.
: >
: > http://c-faq.com/expr/seqpoints.html
: > http://c-faq.com/expr/ieqiplusplus.html
: I've never really understood why
: $i = $i++;
: _needs_ to be undefined in C, C++, or PHP. It seems that in order to assign
: the left side, one needs to evaluate the right side,
No, in order to assign the left side you only need to evaluate that part
of the right side that provides the original (pre-incremented) value.
The increment can happen any time afterwards as long as it's before the
next occasion when the variable is used. In fact if the variable was not
used later then the compiler could skip the increment altogether.
The language definition of C does not restrict the ordering so that
compilers are free to use as many techniques as possible to optimize the
code. Operations such as an increment can be run in parallel with the
rest of a statement (on hardware that has that sort of capability), but
only if the value does not need to be available within the statement
itself.
However, I don't write compilers so what I say is just my interpretation
of other things I've read.
Navigation:
[Reply to this message]
|