Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6622369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:23:49+00:00 2026-05-25T21:23:49+00:00

class Program { static void Main(string[] args) { Test(0); } static void Test(int i)

  • 0
class Program  
{
    static void Main(string[] args)
    {
        Test(0);
    }

    static void Test(int i)
    {
        if (i > 30000)
        {
            return;
        }
        Test(i + 1);
    }
}

Why getting recursive function and throwing StackOverflowException when calling this above sample.

(Because over the default recursive stack size ?)

but i wonder how to solve this problem .

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:23:50+00:00Added an answer on May 25, 2026 at 9:23 pm

    You’re getting an exception because 30,000 stack frames is a rather large number 🙂

    You solve it by using recursion in a more circumspect manner. The ideal problems to be solved recursively are those which reduce the “search space” quickly (a).

    For example, binary tree traversal where your search space is halved each time you recur:

    def find (searchkey, node):
        if node = NULL:
            return NULL
        if searchkey = node.key:
            return node
        if searchkey < node.key:
            return find (searchkey, node.left)
        return find (searchkey, node.right)
    

    Adding two unsigned integers (and your own algorithm above) are not a good fit for recursion because you’ll blow out your stack allocation long before the results are calculated.:

    def add (a, b):
        if a = 0:
            return b
        return add (a-1, b+1)
    

    (a) Search space can be defined as your entire set of possible answers. You want to reduce it as quickly as possible.

    And , as an aside, the ideal problems for recursion have nothing to do with stack space in a theoretical/mathematical sense, they’re just any problem that can be expressed as:

    • the same or similar problem with a “simpler” argument.
    • a terminating condition with the “simplest” argument.

    (“simple” , in this sense, means approaching the terminating condition).

    Theoretical/mathemeatical approaches wouldn’t need to take stack space into account but we, as computer scientists, must. Reality sets limits 🙂

    See also When would I not use recursion? and Situations where you would convert recursion to iteration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Program { internal delegate int CallBack(int i); static void Main(string[] args) { CallBack
Hi I have following test code: class Program { static void Main(string[] args) {
class Program { static void Main(string[] args) { String value = Two; Type enumType
class Program { static void Main(string[] args) { List<Book> books = new List<Book> {
Consider the following code: class Program { static void Main(string[] args) { Department deathStar
Take the following useless program: class Program { static void Main(string[] args) { IUnityContainer
I have the following code: class Program { static void Main(string[] args) { new
I am trying to do the following: class Program { static void Main(string[] args)
In C#, I can do this: class Program { static void Main(string[] args) {
class Program { public delegate void VoidMethodDelegate(); public delegate int IntMethodDelegate(); static void Main(string[]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.