Do fear. Each time you call a function, it has to save the return address to the stack. (That's how the x86 processor works, not the programming language itself.) Additionally, in C, a function is not required to preserve the eax, ecx, or edx registers, so those must be pushed before the call is made. And to lump onto that, since the ebx, esi, and edi registers aren't preserved, they must be pushed at the beginning of a function. (That is unless you don't use them, but only in assembler can you make that distinction.) And while C is different from O'Caml, all the registers must be preserved in practically any language. And to add onto it even more, in many languages, arguments are passed through the stack. So if you were calling a function with a single integer argument, that's likely 8 32-bit things added to the stack right there, for a total of 32 bytes. Add onto that that it has to have memory for local variables for each of those functions. It's not much by itself, but when you have about 10,000 recursions or more, that starts to consume a good amount of memory, not to mention all the processor overhead it takes to work the stack. So to put it basically, don't abuse recursion.Fear not. The antive-code compiler of O'Caml has no stack limit. You can infinitely call a function without problems.
Bookmarks