-
March 11th, 2012, 06:39 PM
#1
How do you bypass Stack Overflow Exception?
public static double RecursiveMeth(double sum, int a)
{
if (a == 100000)
{
return sum;
}
else
{
return RecursiveMeth(sum + 10a, a+1);
}
}
=============================
it works if I put a == 10000
-
March 11th, 2012, 07:46 PM
#2
Re: How do you bypass Stack Overflow Exception?
Simple! Use an iterative approach instead of recursive. Similar to this: http://www.codeproject.com/Articles/...ive-Approaches
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
March 11th, 2012, 08:43 PM
#3
Re: How do you bypass Stack Overflow Exception?
I have to use a recursive method though. It's a homework.
-
March 11th, 2012, 11:06 PM
#4
Re: How do you bypass Stack Overflow Exception?
First, generally speaking, you shouldn't seek to "bypass" exceptions - you should either handle them appropriately, or if they are unexpected, fix the bug that causes them (as in your case).
You're getting a StackOverflowException simply because you have way too many recursive calls.
What is the actual problem you're trying to solve, and also, what is the purpose of the exercise (what is you teacher trying to demonstrate)?
You're probably going wrong about it.
The first thing you should know about recursion is that there must be a way for the method to determine when it should stop calling itself. This is known as the base case - the case your recursive function knows readily how to solve. Otherwise, the you have the recursive case, and the function calls itself, passing along the subproblem it can't readily solve, to be somehow solved by recursion.
Basically, it's the following logic (or some variation of it):
- Figure out a way to decompose the overall problem to more manageable subproblems.
- If it's the base case - solve the simple problem and return the result.
- Else (recursive case) - call the method recursively, and let it somehow* solve the current subproblem.
- When the recursive call returns - integrate the solution of the subproblem with the current overall problem, to finally solve it.
- Return the solution - if the whole, original problem is solved, that's the end; otherwise, the calling function obtains the result and picks up at (4).
*Just a way to think about it - but the path to solution is well defined.
What your program does is basically a really convoluted way to write an equivalent of a for loop, and one that's not working at that.
What are you actually trying to do?
P.S. Please use [code][/code] tags when you post, in order to preserve formatting.
Like this:
Code:
public static double RecursiveMeth(double sum, int a)
{
if (a == 100000)
{
return sum;
}
else
{
return RecursiveMeth(sum + 10a, a+1);
}
}
(Note: the tags do not format previously unformatted code.)
Last edited by TheGreatCthulhu; March 11th, 2012 at 11:14 PM.
-
March 12th, 2012, 05:06 PM
#5
Re: How do you bypass Stack Overflow Exception?
As Cthulhu (I spelled it right!) stated very nicely, you cannot and should not "bypass" a StackOverflowException. It's not something that you can simply ignore and hope everything turns out ok. You have blown your stack, game over.
C# is not capable to tail call optimization. Therefore you always run the risk of blowing the stack if you go too deep. If your instructor requires a recursive solution in C# with no boundary on the size of the input (i.e., the number of recursive calls) then your professor doesn't know the language very well.
If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.
Yes; I have a blog too - http://the-angry-gorilla.com/
-
March 17th, 2012, 03:25 PM
#6
Re: How do you bypass Stack Overflow Exception?
Originally Posted by BigEd781
As Cthulhu (I spelled it right!)
(I noticed this earlier, but then forgot, and only now remembered.)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|