Segmentation fault with fixed PHP 5.3.5 array
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开发者_JAVA百科; $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!
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.
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.
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!
精彩评论