I wrote the following code.
<?php
function f(){
return f();
}
f();
and get the output
$ php test.php
Segmentation fault
Why? I didn’t use any pointers.
This is StackOverflow ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is a case of infinite recursion, but that is not specifically the cause. It is a stack overflow. When you have recursion, whether infinite or not, there is a max amount of depth you can recurse (add to the stack) which is based on the size of your stack (in bytes).
Technically this is infinite, but you won’t get any errors for quite a while:
What you need is known as a
base casein your recursion in order to stop it before it consumes the entire stack.Which will print 0 to 9.
The
segmentation faulterror is coming from the operating system, reporting the PHP application has encountered an issue adding to the stack. That won’t make it to your script because at the system level the binary which makes up PHP has failed.