In Python there is a maximum recursion depth. Seems it is because Python is interpreted rather than compiled. Does C++ have the same concept? Or it is connected only with RAM limit?
- 2I assume you mean a maximum recursion depth. (how deep recursion is allowed to be before you encounter an error)Stack Overflow is garbage– Stack Overflow is garbage2010-04-13 14:01:06 +00:00Commented Apr 13, 2010 at 14:01
- 1I'm not seeing why being an interpreter/compiler would have anything to do with this. For example, Stackless Python is still an interpreter, but doesn't use the C stack for its stack frames and therefore doesn't have the same issue, no?Ken– Ken2010-04-13 14:10:30 +00:00Commented Apr 13, 2010 at 14:10
- note there are C++ and Python implementations that can do tail call eliminations to avoid hitting stack limits in those cases.jk.– jk.2010-04-13 14:11:49 +00:00Commented Apr 13, 2010 at 14:11
- @Ken stackless will still exhaust resources eventually if the recursive call can't be eliminated e.g. tail call optimizationjk.– jk.2010-04-13 14:14:21 +00:00Commented Apr 13, 2010 at 14:14
6 Answers
The limit in C++ is due to the maximum size of the stack. That's typically less than the size of RAM by quite a few orders of magnitude, but is still pretty large. (Luckily, large things like string contents are typically held not on the stack itself.)
The stack limit is typically tunable at the OS level. (See the docs for the ulimit shell built-in if you're on Unix.) The default on this machine (OSX) is 8 MB.
[EDIT] Of course, the size of the stack doesn't entirely help by itself when it comes to working out how deep you can recurse. To know that, you have to compute the size of the activation record (or records) of the recursive function (also called a stack frame). The easiest way to do that (that I know of) is to use a disassembler (a feature of most debuggers) and to read out the size of the stack pointer adjustments at the start and end of every function. Which is messy. (You can work it out other ways – for example, computing the difference between pointers to variables in two calls – but they're even nastier, especially for portable code. Reading the values out of the disassembly is easier IMO.)
11 Comments
help ulimit says that the stack size can be retrieved by running ulimit -s which yields 8192, relative to unlimited yielded by running it without the -s switch. The default limit used if none is provided is -f: the maximum size of files written by the shell and its children. YMMV on other Unixes [Unices?], but I thought it was worth pointing out.No, C++ does not have an explicit recursion depth. If the maximum stack size is exceeded (which is 1 MB by default on Windows), your C++ program will overflow your stack and execution will be terminated.
2 Comments
There's no recursion depth tracking or limit in the C or C++ standards. At runtime, the depth is limited by how big the stack can grow.
2 Comments
ulimit-style runtime limitations, or in the case of threads, any limits set during thread creation. I'm not sure if, by "override the stack", you mean "set it to a size the OS cannot support (e.g. unlimited) because some other limit will be reached first", or "push way too much on the stack and overrun the end." Nothing like having 256K of automatic storage in your recursive function, overshooting an insufficient number of "read only" stack protection pages at the end, and getting a segmentation fault or heap corruption instead of a write-to-read-only-page error...Python has a tunable limit on recursive calls, while C++ is limited by the stack size.
Additionally, many languages or compilers can optimize tail recursion by removing the caller's stack frame so that no additional stack space is consumed. (In tail recursion, the only thing the calling function does is after making the recursive call is to return the recursive call's return value.)
int fact(int n, int accum=1){ if (n==0) return accum; else return fact(n-1,n*accum); //tail recursion here. } Python does not optimize tail recursion (but stackless Python does), and C++ does not require tail recursion optimization, but I believe that gcc optimizes tail recursion. The JVM does not optimize tail recursion, though the Scala language does in certain common documented cases. Scheme and Lisp (and probably other functional languages as well) require that tail recursion be optimized.
Comments
C++ does have a maximum recursion depth, limited by the stack. However, modern operating systems are able to expand a userspace stack dynamically as it fills up, limiting recursion depth by memory space and memory fragmentation only.
5 Comments
boost::regex would use such features if they were available... it makes use of them on Windows.setrlimit(2) describes a RLIMIT_STACK resource, and the man page makes no mention of it being Linux-specific. linux.die.net/man/2/setrlimitboost::regex doesn't do it then. You need to setup a SIGSEGV handler and catch all such exceptions on an alternate stack to handle it. And hope there's no legitimate access violation lying around somewhere.