Reputation: 25812
If we do not-tail-recursive
functions, ocaml will create a stack and push info inside. And it is possible to get stack overflow
error if we recursively call too many times.
So what's the threshold? What's the size of the function stack?
Upvotes: 2
Views: 1495
Reputation: 66803
For the bytecode interpreter, the documentation says the default size is 256k words. (I think wordsize is 32 or 64 bits depending on the system.) You can adjust it with the l
parameter in OCAMLRUNPARAM
or through the GC module.
For native code, the documentation says that the native conventions of the OS are used. So it will be different for each implementation.
I just looked these things up now; I've never needed to know in practice. Generally I don't want to write code that gets anywhere near the stacksize limit.
Upvotes: 3
Reputation: 35983
I don't know this for sure, but it is clear that the recursion depth is dependent on the function you are talking about. Simply consider these two (non-tail-recursive) functions:
let rec f x = print_int x; print_char '\n'; 1 + f (x+1);;
let rec g x y z = print_int x; print_char '\n'; 1 + g (x+1) y z;;
And try f 0
resp. g 0 0 0
. Both functions will eventually produce a stack overflow, but the latter (g
) will do so "earlier".
It may be the case that there is a certain number of bytes available on the stack. You can probably approximate this number by looking at how far f
goes and looking up what exactly is pushed onto the stack when a function call occurs.
Upvotes: 1