34

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 recursive calls. So I need probably several 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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Frank
  • 7,235
  • 9
  • 46
  • 56

4 Answers4

47

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
  • 1,645
  • 13
  • 15
  • 4
    For those who go the editbin route, you need to download the Visual C++ toolset in the Visual Studio installer. You can also put it in your post build steps as `EDITBIN.EXE /STACK: $(TargetName)` – Cameron Aavik Mar 18 '17 at 12:42
  • 1
    @CameronAavik Adding to your comment, I needed to add the SDK tools folder to my path, restart Visual Studio to see this change, and use `$(TargetPath)` instead of `$(TargetName)`. – Jamie Thomas Sep 07 '17 at 13:29
  • @JamieThomas Adding to your comment in turn, instead of changing path, you can include in post-build event command line: call "$(DevEnvDir)..\tools\VsDevCmd.bat" – mbabramo Jun 27 '19 at 15:55
  • Qualifying my own comment: That may not always work. Setting PATH environment variable to include folder containing editbin will work. Command can then be included within Exec element with Target element with Name="PostBuild" and AfterTargets="PostBuildEvent" in the .csproj file. – mbabramo Oct 02 '19 at 12:40
20

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
  • 554,122
  • 78
  • 1,158
  • 1,373
9

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

John Boker
  • 82,559
  • 17
  • 97
  • 130
  • Let's be a little more precise: The problem isn't so much the recursion *per se*, or the number of iterations (after all, there will still be 2 billion iterations after the conversion to a loop). The real issue is that the compiler might not generate tail calls; only because of that does recursion combined with the high number of iterations become problematic. – stakx - no longer contributing Aug 30 '12 at 16:55
-1

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.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 1
    In general, I don't think the .NET compilers will optimize a tail-recursive call. At least, C# and VB.NET don't. – user1172763 May 31 '16 at 21:26