Bad Code (was: Re: [PHP] ++ BUG??)
| From: | Ignacio Vazquez-Abrams | Date: | Sun, 26 Nov 2000 04:42:01 +0000 |
| Subject: | Bad Code (was: Re: [PHP] ++ BUG??) | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-27230@lists.php.net to get a copy of this message | ||
On Sat, 25 Nov 2000, Jason wrote:
> I tested it in C and you're right, it is indeed well defined. I was slightly
> off in my interpretation of when, exactly, the ++ operators increment the
> variable. I thought it was after the use of the variable, not after the entire
> statement is finished. So I'll concede that it's a bug if they're going for
> C-style behavior.
>
> All I was trying to say, though, was that it's bad code and the bug is easily
> avoided :)
>
I really do hate to continue this thread, but I don't necessarily agree with
the "bad code" part. Compilers and interpreters can treat them very
differently. Note: the following examples are on CPUs which do not have a pure
load/store architecture and assume that there is no other code.
i=(i+1)%2:
1) load memory location I into register X
2) add 1 to register X
3) take modulus 2 of register X
4) store register X into memory location I
i=(i++)%2:
1) take modulus 2 of memory location I
2) increment memory location I
i=(++i)%2:
1) increment memory location I
2) take modulus 2 of memory location I
The memory location operations would have to be quite expensive to make the
first example faster.
--
Ignacio Vazquez-Abrams <ignacio@openservices.net>