How to allow more memory and avoid stack overflow on lots of recursion?

前端 未结 5 2009
-上瘾入骨i
-上瘾入骨i 2021-01-06 19:33

I\'m testing the timing of an algorithm that does lots of recursive calls. My program dies at about 128k recursive calls, and this takes only .05 seconds. I\'d like to allow

5条回答
  •  囚心锁ツ
    2021-01-06 20:08

    You have three options:

    1. Rewrite the program to make it non-recursive. This is the best, but not always possible.

    2. Rewrite the program to use a separate stack for storing the execution state. This way you preserve the recursive nature but no longer use the system stack for storing the recursion algorithm state data.

    3. Tweak the environment to postpone the inevitable. Visual C++ has a linker setting for the stack size. Almost sure gcc has one too.

提交回复
热议问题