3

When trying to define an array such this:

$array = new SPLFixedArray(256);

for ($i = 0; $i < 256; $i++) {
    $array[$i] = new SPLFixedArray(256);

    for ($j = 0; $j < 256; $j++) {
        $array[$i][$j] = new SPLFixedArray(5);

        for ($k = 0; $k < 5; $k++) {
            $array[$i][$j][$k] = 0;
        }
    }
}

I'm getting, only in CLI, "Segmentation Fault". I read about such errors here on SO in C/C++, where is likely to be a memory problem and recommends to load everything to the heap memory with malloc(). In PHP do we have such tool?

This happens even in small 3d arrays, such as 15 instead of 256 (but works under 15).

Thanks!

3
  • php version would be good to specify in such kind of questions. Commented Jun 15, 2011 at 1:49
  • No such tool within PHP userland. Please report this on bugs.php.net when it is back up. It's not going to get answered as SO question. Commented Jun 15, 2011 at 2:14
  • It appears to be fixed in PHP 5.3.7-dev. Commented Jun 15, 2011 at 4:16

3 Answers 3

5

Only a PHP bug would segfault; you should never be able to do that. It segfaults for me on PHP 5.3.5. I see nothing in 5.3.6's change log that would indicate it has been fixed. (It crashes on 5.3.6 for me as well.)

As a workaround you could do this:

$array = new SplFixedArray(256 * 256 * 5);
$array[$i * JK + $j * K + $k] = $foo;

JK and K are constants. JK = $jsize * $ksize; and K = $ksize.

That's likely to give you better performance than creating 3D arrays anyway.

Update:

I tried it on PHP 5.3.7-dev, and there is no segfault. So, fingers-crossed, it has been fixed and will work correctly in PHP 5.3.7.

Sign up to request clarification or add additional context in comments.

Comments

1

You did not say what PHP version you are running. Not sure if this can be related but SPLFixedArrays had a bug posted, a fix was ported in snapshot at beginning of June.

You can try out your code against the snapshots and see if it solves your issue, Linux snapshot or Windows Snapshot.

Comments

0

The segfault is likely coming from the Operating System level telling you that you've busted the stack. This being said, the reason why you busted your stack is from the multiple invocations of SplFixedArray() that you invoked from the nesting of your loops. This is the reason why, like you mentioned, in small 3d arrays, it'll get busted as well.

That being said, try not to have such nestings if possible. This is equivalent to having an infinite recursion. Not to mention that PHP's underlying mechanism is still C.

Hope it helps! Cheers!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.