FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » simple session question
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: simple session question [message #175742 is a reply to message #175741] Sun, 23 October 2011 01:13 Go to previous messageGo to previous message
Richard Damon is currently offline  Richard Damon
Messages: 58
Registered: August 2011
Karma:
Member
On 10/22/11 8:12 PM, Thomas Mlynarczyk wrote:
> Richard Damon schrieb:
>
>> I suspect that there is a difference between the execution model of
>> C/C++ and PHP here, do in part to the fact that C/C++ is (normally) a
>> compiled language with the goal of allowing the compiler to generate
>> as efficient of code as possible, while PHP is designed as a
>> interpreted language.
>
> Hm. But PHP is written in C, so I would assume it to just follow C here.
>
>> x = x; /* perform the = */
>> x = x+1; /* perform the ++ */
>
> That could be further optimized by dropping the x = x. But here the
> assignment seems to have a higher precedence than the increment.
>
>> temp = x;
>> x = x+1;
>> ... do what ever with temp
>>
>> Which being an interpreted language makes some sense, why put off
>> doing something, and recording somewhere that you need to do it, when
>> you can do it now, the possible savings that C might have been able to
>> make, get swamped by the other overhead in PHP.
>
> Now I wonder why x = x++ is undefined in C. If it was defined to be a no
> op as it behaves in PHP, then it could be optimized away completely.
>
> Greetings,
> Thomas
>

The issue is C is that there is a general rule (to allow optimizations)
that defines the behavior to be undefined if an expression causes a
variable to be written to twice, or have a read from and write to (where
the read from is not needed to determine the value to write to) without
an intervening sequence point, like the end of an expression.

The statement x = x++; has two different writes to x, so we meet the
requirement for undefined behavior. Not also that C does not limit when
the ++ part happens, only that this side effect will finish by the next
sequence point.

Part of the problem with this example is that it is a bit to simple, and
that simplicity hides some of the issues. Let us make the expression
just slightly more complicated to make it cleared. Let us use x = 5*x++;

This expression has two different value paths:

x = 5 * x; (the main expression) and
x = x + 1; (the side effect of x++)

The only ordering that C puts on these two expressions is that the read
for x in the first, must happen before the write of x in the second (due
to the definition of x++).

Thus in C the net expression is likely to be one of:

x = x+1; (if the first expression finishes first, and then the second,
using the original value of x)
x = 5*x; (if the second expression finishes first, and then the first,
using the original value of x)
x = 5*x+1; (if the first expression finishes first, and then the second,
using the new value of x)

It is even possible on some machines that the program will trap, as C
doesn't limit the possible effect on undefined behavior. This might
happen on a machine which is pipelined, and there is a restriction on
either writing a value and reading it back to quickly, or doing two
writes to closely.

This example may seem simple enough that the compiler should catch it,
but what if the statement was:

*y = x++;

and y just happened to point to x.

PHP, not trying to allow the compiler to generate as efficient code,
doesn't need to allow for the ambiguity in when the increment occurs,
and it makes sense to do it right away. Knowing C, I don't see them
making that explicit promise, but the way it is worded, if you didn't
know C, it seems to be what would be expected.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: session cookie: client side
Next Topic: by get this format my explode file name like this through php
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Dec 22 02:12:04 GMT 2024

Total time taken to generate the page: 0.05307 seconds