Without using recursion how can a stack overflow exception be thrown?

前端 未结 10 1953
自闭症患者
自闭症患者 2021-01-18 04:19

Without using recursion how can a stack overflow exception be thrown?

相关标签:
10条回答
  • 2021-01-18 05:07

    Every method call that has not yet returned consumes some stack space. (Methods with more local variables consume more space.) A very deep call stack can result in stack overflow.

    Note that on systems with limited memory (mobile devices and such) you don't have much stack space and will run out sooner.

    0 讨论(0)
  • 2021-01-18 05:08

    Declare an ENORMOUS array as a local variable.

    0 讨论(0)
  • 2021-01-18 05:10

    Short answer: if you have an object which calls an internal object, you increase the stack trace by 1. So, if you have 1000s of objects nested inside one another, each calling its internal object, eventually you'll get a stack overflow.

    Here's a demonstration of how to generate primes using nested iterators:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Program p = new Program();
    
                IEnumerator<int> primes = p.AllPrimes().GetEnumerator();
                int numberOfPrimes = 1000;
                for (int i = 0; i <= numberOfPrimes; i++)
                {
                    primes.MoveNext();
                    if (i % 1000 == 0)
                    {
                        Console.WriteLine(primes.Current);
                    }
                }
                Console.ReadKey(true);
            }
    
            IEnumerable<int> FilterDivisors(IEnumerator<int> seq, int num)
            {
                while (true)
                {
                    int current = seq.Current;
                    if (current % num != 0)
                    {
                        yield return current;
                    }
                    seq.MoveNext();
                }
            }
    
            IEnumerable<int> AllIntegers()
            {
                int i = 2;
                while (true)
                {
                    yield return i++;
                }
            }
    
            IEnumerable<int> AllPrimes()
            {
                IEnumerator<int> nums = AllIntegers().GetEnumerator();
                while (true)
                {
                    nums.MoveNext();
                    int prime = nums.Current;
                    yield return prime;
    
                    // nested iterator makes a big boom     
                    nums = FilterDivisors(nums, prime).GetEnumerator();
                }
            }
        }
    }
    

    There's no recursion, but the program will throw a stack overflow exception after around 150,000 primes.

    0 讨论(0)
  • 2021-01-18 05:22

    If you call enough methods, a stack overflow can occur anytime. Although, if you get stack overflow errors without using recursion, you may want to rethink how you're doing things. It's just so easy with recursion because in an infinite loop, you call a ton of methods.

    0 讨论(0)
提交回复
热议问题