CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2012
    Posts
    13

    Question 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

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    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.

  3. #3
    Join Date
    Feb 2012
    Posts
    13

    Re: How do you bypass Stack Overflow Exception?

    I have to use a recursive method though. It's a homework.

  4. #4
    Join Date
    Jan 2010
    Posts
    1,133

    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):
    1. Figure out a way to decompose the overall problem to more manageable subproblems.
    2. If it's the base case - solve the simple problem and return the result.
    3. Else (recursive case) - call the method recursively, and let it somehow* solve the current subproblem.
    4. When the recursive call returns - integrate the solution of the subproblem with the current overall problem, to finally solve it.
    5. 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.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    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/

  6. #6
    Join Date
    Jan 2010
    Posts
    1,133

    Re: How do you bypass Stack Overflow Exception?

    Quote Originally Posted by BigEd781 View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured