Hmm, will probably have to look inside PHP for this ... the foreach
loop will copy each element as it loops over it (without actually
copying, obviously), however there's no change happening to the
element at any point and so there's nothing to suggest to the
copy-on-write to create a new instance of the sub-array.

It should look like this:
$a = array(0, 1, 2, array(0, 1, 2, 3), 4, 5, 6, .... n);
$b = $a[3];

Whether or not you loop over $a and thus move the internal pointer,
you don't change (well, shouldn't, anyway) $b as that's a subarray
which has it's own internal pointer, that isn't touched.

Or maybe I've gotten this completely backwards ...


On 16 March 2010 17:12, Robert Cummings <> wrote:
> Peter Lind wrote:
>> This is one example where references actually decrease memory usage.
>> The main reason is the recursive nature of the function. Try
> BTW, it's not the recursive nature of the function causing the problem. It's
> the movement of the internal pointer within the array. When it moves the COW
> realizes the copy's pointer has moved and splits off the copy. You can
> verify this by echoing the memory usage when you first enter carray(). The
> spike occurs inside the foreach loop.
> Cheers,
> Rob.
> --
> Application and Templating Framework for PHP
> --
> PHP General Mailing List (
> To unsubscribe, visit:

WWW: /
BeWelcome: Fake51
Couchsurfing: Fake51

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to