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?
--
Jesper Stocholm http://stocholm.dk
Give a man a fish and he will have food for a day,
give a man an elephant, and he will have food for a week. 4 3836
Nesting function calls a million levels deep is hardly ever a good idea, and
I don't see anything in this function that would require recursion. It looks
like you're applying SHA1 to an array of bytes over and over again and not
doing anything with the result except for timing it. You should be able to
replace your recursive function with a simple loop.
If you really do need to use recursion for some reason that's not
illustrated here, you can cut down on the number of parameters you put on
the stack for each call. Based on what the function actually does, you could
make them all member variables and not pass any of them on the stack.
"Jesper Stocholm" <j@stocholm.invalid> wrote in message
news:Xn***********************@192.38.208.86... 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?
-- Jesper Stocholm http://stocholm.dk Give a man a fish and he will have food for a day, give a man an elephant, and he will have food for a week.
A case for C# tailcalls? ;)
The managed stack is only 1MB by default (AFAIK). So, if you do enough
recursion to allocate more than that, you'll get this exception. I'd
suggest moving the recursion to inside one method.
-mike
MVP
"Jesper Stocholm" <j@stocholm.invalid> wrote in message
news:Xn***********************@192.38.208.86... 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?
-- Jesper Stocholm http://stocholm.dk Give a man a fish and he will have food for a day, give a man an elephant, and he will have food for a week.
If you can't re-write the method as Bret suggested, you can take the easy
way out and just significantly up the stock size (if you have the editbin
tool available): http://blogs.geekdojo.net/richard/posts/207.aspx
--
Veuillez m'excuser, mon Français est très pauvre. Cependant, si vous voyez
mauvais C #, c'est mon défaut!
"Jesper Stocholm" <j@stocholm.invalid> wrote in message
news:Xn***********************@192.38.208.86... 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?
-- Jesper Stocholm http://stocholm.dk Give a man a fish and he will have food for a day, give a man an elephant, and he will have food for a week.
Bret Mulvey [MS] wrote : Nesting function calls a million levels deep is hardly ever a good idea, and I don't see anything in this function that would require recursion. It looks like you're applying SHA1 to an array of bytes over and over again and not doing anything with the result except for timing it. You should be able to replace your recursive function with a simple loop.
Yes - what I am doing is creating a chain of hash-values, so the output
of SHA1 is in the next recursion used as input to SHA1.
If you really do need to use recursion for some reason that's not illustrated here, you can cut down on the number of parameters you put on the stack for each call. Based on what the function actually does, you could make them all member variables and not pass any of them on the stack.
I re-wrote the recursive method to a FOR-loop instead that updates a
member variable. It now works like a charm.
Thanks, :)
--
Jesper Stocholm http://stocholm.dk
Give a man a fish and he will have food for a day,
give a man an elephant, and he will have food for a week. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tim Tyler |
last post by:
I'm getting fatal errors when executing code - and my error handler is
failing to trap them - so I get no stack backtrace :-(
The error I am getting is:
"Fatal error: Call to a member function...
|
by: Aguilar, James |
last post by:
Hello all,
To begin, yes, this -is- a homework assignment. However, it is for my
Algorithms class, and my instructor has given us explicit permission to use
"expert" groups like newsgroups, so...
|
by: Jim |
last post by:
I have spent the past few weeks designing a database for my company.
The problem is I have started running into what I believe are stack
overflow problems. There are two tab controls on the form...
|
by: Victor |
last post by:
Hello,
I've got a situation in which the number of (valid) recursive calls I
make will cause stack overflow. I can use getrlimit (and setrlimit)
to test (and set) my current stack size. ...
|
by: pillars |
last post by:
I have been developing an application in Windows CE.Net using the .Net
Compact Framework and C#. Data is saved to a local Sql Server CE database and
then transferred wirelessly to a central server....
|
by: David W. Walker |
last post by:
I am attempting to port a C code that runs OK on a number of Linux and Unix
machines to Windows XP using Visual Studio C++. I have set the program up as
a console application, but when I try to run...
|
by: CBFalconer |
last post by:
We often find hidden, and totally unnecessary, assumptions being
made in code. The following leans heavily on one particular
example, which happens to be in C. However similar things can (and...
|
by: Ali |
last post by:
Hi,
I got stack overflow errors while using an unmanaged dll from managed c#
application. When i use editbin.exe to increase stack size of app , it works.
Is there a way to increase stack size of...
|
by: amit.atray |
last post by:
Environement : Sun OS + gnu tools + sun studio (dbx etc)
having some Old C-Code (ansi + KR Style) and code inspection shows
some big size variable (auto) allocated (on stack)
say for ex.
char...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |