views:

246

answers:

4

I have a program that does recursive calls for 2 billion times and the stack overflow. I make changes, and then it still need 40K resursive calls. So I need probably serveral MB stack memory. I heard the stack size is default to 1MB. I tried search online. Some one said to go properties ->linker .........in visual studio, but I cannot find it.

Does anybody knows how to increase it? Also I am wondering if I can set it somewhere in my C# program?

P.S. I am using 32-bit winXP and 64bit win7.

+6  A: 

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

John Boker
+1, 2 billion recursive calls is a bit excessive.
Carl Norum
+9  A: 

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.

Reed Copsey
+5  A: 

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
Andrew O'Reilly
hi, can you please explain how to run the EDITBIN command because I have no clue. [email protected]
mouthpiec
A: 

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.

DeadMG