   [.net] StackOverflowException in debug builds - Why so early?
  Hello all. Please take a look at the following snippet:    class Program    {   static void Main(string[] args)   {  for (int i = 1000; ; i += 500) StupidPrint(i.ToString(), 0, i);   }   static void StupidPrint(string str, int level, int printAt)   {  if (level++ >= printAt) Console.WriteLine(str);  else StupidPrint(str, level, printAt);   }    }This throws a StackOverflowException at somewhere in the range 14500-15000. The ints are 4 bytes each, and I don't know what a reference is, but lets say 32 bytes. Lets also add 32 bytes for frame pointers and stuff. That should give us something like 72 bytes per call, times 15000 calls. That's just over 1MB! Why the hell does it crash that early? Is there any way to solve this (as in increasing stack size)?This only affects debug mode, in release mode, it keeps going for at least 350000000 calls with almost no ram usage.. Is it optimizing away the recursion?   
  Your recursion never terminates, so it doesn't matter how large the stack is. You're going to overflow it eventually until someone invents a computer with infinite memory.   ;  It looks like your recursion DOES terminate, but your main loop doesn't! (and as it grows larger so does the recursion depth)What is it that you are trying to accomplish here? Maybe we can help you do it a different way.   ;  This is just a test app to see when it crashes. The real app is a university assignment which does (would) terminate.An iterative solution on the same data/structure work great but a recursive solution overflows. It's a formal requirement to solve the assignment by recursion. The snippet is just to test/show how deep the recursion goes for calls comparable with those in the assignment.I'm quite sure the code is correct (inefficient perhaps, but correct as in should give the right results). ;Quote:Original post by DvDmanDTThat's just over 1MB! Why the hell does it crash that early?The default stack size is 1MB. As far as I know, there's no way to change the stack reserve size as part of a C# project's properties, but you can modify the stack size of a PE with editbin and the /stack switch.;//this never terminates, and as "i" becomes larger, your recursive call goes deeperfor (int i = 1000; ; i += 500)so no, it's not correct!   ;  You missed the point. This app should crash since it's made for testing at which recursion depth it crashes. You can do this if it you want to, still crashes at i=15000 or so..    class Program    {   static void Main(string[] args)   {  for (int i = 1000; i < 1<<30; i += 500) StupidPrint(i.ToString(), 0, i);   }   static void StupidPrint(string str, int level, int printAt)   {  if (level++ >= printAt) Console.WriteLine(str);  else StupidPrint(str, level, printAt);   }    };Quote:Original post by SiCraneThe default stack size is 1MB. As far as I know, there's no way to change the stack reserve size as part of a C# project's properties, but you can modify the stack size of a PE with editbin and the /stack switch.Thanks alot, this indeed works. Now, I'm trying to do this at post-build-events, but I keep getting a 9009 exit code from editbin. Any ideas?;  If I recall correctly you get that if editbin isn't on your PATH. I believe the fix is to modify the post-build event to call the vsvars32.bat for your development environment before calling editbin. Something like:call "$(DevEnvDir)..\tools\vsvars32.bat"editbin /STACK:2048 "$(TargetPath)";  Thanks, I used the full path and it crashed the incremental linker instead, so now I use this:    class Program    {   static void Main()   {  Thread th = new Thread(RealMain, 1 << 28); // 28 = 256MB, 30 = 1024MB  th.Start();  th.Join();   }   static void RealMain()   {  for (int i = 1000; ; i += 500) StupidPrint(i.ToString(), 0, i);   }   static void StupidPrint(string str, int level, int printAt)   {  if (level++ >= printAt) Console.WriteLine(str);  else StupidPrint(str, level, printAt);   }    }Seems to work.   