12

i wonder how much calls we can perform in stack in c# before we get stack overflow exception

so i decided to write the following code

    static void Method2(int Calls)
    {
        if(!Calls.Equals(0))
            Method1(--Calls);//if more calls remain call method1 and reduce counter
    }
    static void Method1(int Calls)
    {
        if (!Calls.Equals(0))//if more calls remain call method2 and reduce counter
            Method2(--Calls);

    }
    static void Main(string[] args)
    {
        var Calls= 42994;//number of calls(stack overflow appears for large number)
        Method1(Calls);
    }

my question is how compiler decides to throw stack overflow exception is this about memory limitations? once i put 42995 i got stackoverflow but this number is not constant so how this works?

3
  • 7
    Technically, the compiler does not throw this exception, the runtime does. Commented Apr 23, 2015 at 19:10
  • well... how this happens?@BradleyDotNET Commented Apr 23, 2015 at 19:11
  • 2
    There is a constant amount of space in the stack, not a constant number of function calls.
    – Gabe
    Commented Apr 23, 2015 at 19:17

1 Answer 1

11

Each thread has a stack size. The predefined stack size for the main thread of a program is fixed in the exe file. Each recursive call you make, you consume a little of this stack. When you finish it, the CLR throws a StackOverflowException. For Console/Graphical programs the default stack size should be 1mb of memory. You can't make this memory "bigger" from inside the program (you can use editbin.exe to change it from "outside" the program). This memory isn't dynamic. It is fixed (technically, the address space reserved for this memory is fixed, the memory is really allocated by the Windows OS on demand, probably 4kb at a time, but always up to the reserved address space). You can create secondary threads with the stack size you want.

Note that the handling of the stack in this way is a limitation of x86/x64 architecture, http://en.wikipedia.org/wiki/Stack-based_memory_allocation:

Some processors families, such as the x86, have special instructions for manipulating the stack of the currently executing thread. Other processor families, including PowerPC and MIPS, do not have explicit stack support, but instead rely on convention and delegate stack management to the operating system's application binary interface (ABI).

4
  • 1
    does size of parameters affect size of stack or variable inside methods? Commented Apr 23, 2015 at 19:17
  • 1
    The stack is filled primarily by three things in the default implementation of .NET: the local variables that aren't "closed" by anonymous functions/yield/async (these other variables are moved to a separate class), some data for the stack trace, some data for finally/catch. Note that a reference type occupies on the stack only the size of the reference, so (local variable) int[] a = new int[100] occupies the space of a reference on the stack, plus the size of new int[100] somewhere in the heap.
    – xanatos
    Commented Apr 23, 2015 at 19:19
  • thanks for the answer ;)...i wish .net was more intelligent...for example finding an infinite loop between calls and then throw exception @xanatos Commented Apr 23, 2015 at 19:23
  • 4
    @M.kazemAkhgary, read about Halting problem.
    – n0rd
    Commented Apr 23, 2015 at 19:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.