Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StackOverflowException in c# when no local variable in the function

when i do this

static void Main()
{
   Main();
}

I receive stackoverflow exception. As i have read so far about C# they say ONLY local variable of value types (and short living ones) will go on stack.

But here in the code there are no local variable to go on stack then what overflows it ?

I know from assembly code line Perspective that reference to Main() will go on stack too ? Is that right ?

like image 284
Dhananjay Avatar asked Jan 27 '26 18:01

Dhananjay


2 Answers

Each time you call main(), the return address is saved in the stack, which will eventually cause your stack to blow up

like image 160
Shai Avatar answered Jan 30 '26 08:01

Shai


Each function/method call uses stack to store the return location. Here, you have an infinite recursion which eventually stops because stack space gets exhausted.

like image 23
zvrba Avatar answered Jan 30 '26 06:01

zvrba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!