WRT my question: Code execution breaking during recursion
I don't want to increase the stack size for every application running in my account. Just one C++ executable.
What is the way to do it?
WRT my question: Code execution breaking during recursion
I don't want to increase the stack size for every application running in my account. Just one C++ executable.
What is the way to do it?
If you really need huge amount of stack space you can make use of Boost Context and temporarily allocate a custom amount of stack space. This works on a per-thread level so it might not answer your question. However, as already said in comments, you should probably improve your algorithm as increasing stack size is hardly ever needed.
#include <boost/context/fixedsize_stack.hpp>
#include <boost/context/continuation.hpp>
void recurse(int depth)
{
std::cout << depth << std::endl;
recurse(depth + 1); // infinite recursion to test how far you can go
std::cout << depth << std::endl; // just to avoid tail recursion optimization.
}
int main(int argc, char* argv[])
{
namespace ctx = boost::context;
ctx::fixedsize_stack my_stack(100 * 1024 * 1024); // adjust stack size for your needs
ctx::continuation source =
ctx::callcc(std::allocator_arg, my_stack, [](ctx::continuation&& sink) {
// everything in here runs within a context with your custom stack.
recurse(1);
return std::move(sink);
});
// This starts the context and executes the lambda above.
source = source.resume();
return 0;
}
On Linux using GCC or Clang you could also replace fixedsize_stack
with segmented_stack, which automatically grows until you run out of memory.
Boost Context is portable among most major platforms.