Helber Galarga

Helber Galarga

  • NA
  • 3
  • 4.2k

recursion with StackOverflowException

Aug 30 2010 10:59 PM
Hi

I am new in this forum, and have some trouble regarding StackOverflowException

suposse I would want to control StackOverflowException originated by calling a recursive method,  that gets the call stack over it's defaults capabilites.

-in my case for my homework this 'd be a call to Ackermann(3,10)- . And... Knowing that is not possible to control StackOverflowException since C# 2.0...

Since I don't know if it is possible in C# to modify memory propierties of the call stack, the only solution left -if it isn't posibly to grow bigger the call stack- would be control thru reading the call stack state, before it overflows...

I 've been told that using threads -tru delegates- and editing its memory properties can help in this case, but... I'm having problems trying this...

 namespace Ackermann
{
class AckermannREC
{


public static long Ackermann(long m, long n)
{
if (m > 0)
{
if (n > 0)
return Ackermann(m - 1, Ackermann(m, n - 1));
else if (n == 0)
return Ackermann(m - 1, 1);
}
else if (m == 0)
{
if (n >= 0)
return n + 1;
}
throw new System.ArgumentOutOfRangeException();
}


static void Main()
{
long m = 3, n = 9;

Console.WriteLine("Ackermann({0}, {1}) = {2}", m, n, Ackermann(m, n));
Console.ReadKey();
}


}
}



Any help or suggestion would be appreciated...

Answers (2)