J
Jesper Stocholm
I have a recursive function that I would like to do a lot of recursive
calls (preferebly 2^20 in total)
The function is called as (with maxi = e.g. 100000)
DoRecursion(0,maxi,bseed,sha);
And the total code is
static void Main(string[] args)
{
long maxi = Convert.ToInt64(args[0]);
try
{
SHA1 sha = new SHA1CryptoServiceProvider();
// initial seed
string seed = System.Guid.NewGuid().ToString();
byte[] bseed = (new UTF8Encoding()).GetBytes(seed);
System.DateTime time1 = DateTime.Now;
DoRecursion(0,maxi,bseed,sha);
System.DateTime time2 = DateTime.Now;
// interval is created
System.TimeSpan interval = time2 - time1;
Console.WriteLine("Milliseconds: \t {0}",interval.TotalMilliseconds.ToString());
}
catch (Exception e)
{
Console.WriteLine("Error: {0}",e.Message);
}
}
private static void DoRecursion(long i, long maxi, byte[] seed, SHA1 sha1)
{
try
{
byte[] result;
result = sha1.ComputeHash(seed);
if (i < maxi)
{
i++;
DoRecursion(i,maxi,result,sha1);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
But every time I reach a value 15196 for i, I get the error "Fatal stack
overflow error."
What am I doing wrong?
calls (preferebly 2^20 in total)
The function is called as (with maxi = e.g. 100000)
DoRecursion(0,maxi,bseed,sha);
And the total code is
static void Main(string[] args)
{
long maxi = Convert.ToInt64(args[0]);
try
{
SHA1 sha = new SHA1CryptoServiceProvider();
// initial seed
string seed = System.Guid.NewGuid().ToString();
byte[] bseed = (new UTF8Encoding()).GetBytes(seed);
System.DateTime time1 = DateTime.Now;
DoRecursion(0,maxi,bseed,sha);
System.DateTime time2 = DateTime.Now;
// interval is created
System.TimeSpan interval = time2 - time1;
Console.WriteLine("Milliseconds: \t {0}",interval.TotalMilliseconds.ToString());
}
catch (Exception e)
{
Console.WriteLine("Error: {0}",e.Message);
}
}
private static void DoRecursion(long i, long maxi, byte[] seed, SHA1 sha1)
{
try
{
byte[] result;
result = sha1.ComputeHash(seed);
if (i < maxi)
{
i++;
DoRecursion(i,maxi,result,sha1);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
But every time I reach a value 15196 for i, I get the error "Fatal stack
overflow error."
What am I doing wrong?