How to change stack size for a .NET program?

社会主义新天地 提交于 2019-11-26 15:24:53

The easiest way to set the stack size from .NET 2.0 and Win XP onwards is to spawn a new thread with the stack size you'd like:-

using System.Threading;

Thread T = new Thread(threadDelegate, stackSizeInBytes);
T.Start();

To change the stack size of the entire program you'd have to use editbin:-

EDITBIN.EXE /STACK:<stacksize> file.exe

There is no compiler option to do it. You can edit it after the fact using editbin /stack, or create a separate thread for your algorithm, and specify a larger stack size in the Thread constructor.

That being said, you may want to flatten your recursive function... If you're having stack overflows now, it's tough to know that any stack size will be appropriate in the long term. This is just a band-aid solution.

Most likely you should try to use loops instead of recursion.

I know that in VS you can set an arbitrary stack size (EDIT: For C++ programs). However, I'd suggest that you use a tail call (i.e., return MyFunc(args); ) which automatically recycles stack space. Then, you'd use some heap-allocated object to hold state.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!