Limited Number of Active Methods?
I am currently working on a recursive program to process datasets of varying sizes.
The problem I'm finding is that wi开发者_如何学编程th my largest dataset (around 350,000 records) my program seems to be terminating without providing any error information.
From testing I think it could be due to the recursion leaving too many incomplete methods open (rough count is 72756). So my question is, does C++ have a limit to the number of active methods or am I looking at the wrong issue?
C++ as a language does not. A real implementation of C++ will always have a limit though, and you're using deep enough recursion that running out of stack space certainly sounds quite possible.
At least right off, it sounds like recursion is probably a poor choice for the job, though you've given little enough description that it's a bit hard to say.
No particular limit from the language's point of view. However, all implementations are limited by the available resources.
If you have very deep recursions, you might run out of stack space. Check your compiler/linker settings for requesting additional space.
There is a limit to recursion depth, if that's what you're asking. Every time you call a function, some more space on the stack is required to hold that function's arguments and local variables. This stack space won't get reused until you return from that function. If you're recursing 70k levels deep I would believe that you're running out of stack space. Depending on OS/threads stack space is relatively limited and it'd be hard to portably increase it. You might have to use a stack data structure (STL has one) and not use recursion to avoid this problem. Memory that's allocated through new/malloc doesn't have the same limitations that stack memory does, so you can allocate just about as much as is available in your system.
As as been suggested already, you are probably running out of stack space, which means that you are running in a StackOverflow. You're on the right site to ask about it :)
Each time you call a function, the program will reserve some stack space to store the argument with which the functions is called and its local variables. This quickly adds up, and with a depth of 72k methods, you're probably blowing the reserved space out.
There are various possible solutions:
Compile with optimizations on, optimizations may reduce stack consumption by optimizing variables out and use CPU registers more intensively... and thus you may consume less space for the same program.
Poke into your compiler's documentation, there should be an option to augment the amount of memory reserved for the stack. For gcc it's
-Wl,--stack,<bytes>
(it's a linker option). For gcc the default is 8MiB already, but you can go for 16 or 32 easily, we're not that limited nowadays.Some compiler (at least gcc 4.6) have a
-fsplit-stack
. It's more or less experimental still, but the idea is to allow a stack in multiple parts, which grows on demand. If it works, it'll solve your problem right away.
There are also various strategies to limit the problem at the source:
- use iterative algorithms
- push the deeply recursive algorithms into another thread (so that they have a fresh stack to begin with)
It's up to you to figure out the more practical solution for you own case.
精彩评论