-2

I'm watching a C# tutorial and came across StackOverflowException.

the narrator gave a neat example of such an exception using the code snippet below,

public class Employee
{
    private string _name;

    public string Name
    {
       get{ return Name; }
    }
}

I'm looking for some example of this type of simple code in C++ and Java and more specially in javascript that can cause Stack Overflow.

Hossain Cyrus
  • 65
  • 2
  • 5

4 Answers4

13

This Explanation is the basic reason behind a StackOverflowException for Java, C, and C++.

A stack overflow exception is generally caused in any language due to recursive method calls.

Suppose you have an method which is calling itself or any other method for an infinite, recursive loop; it will cause a Stackoverflowexception. The reason behind this is the method call stack gets filled, and it won't be able to accommodate any other method call.

Method call stack looks like this picture.

enter image description here

Explanation -- Suppose the Main method has five statements, and the third method has an call to methodA, then the execution of Main method gets paused at statement3 and methodA will be loaded into the call stack. Then method A has a call to methodB. So methodB also gets loaded into the stack.

So in this way infinite, recursive calls make the call stack get filled at which point it can't afford any more methods. So it throws a StackOverflowException.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Nikhil Agrawal
  • 26,128
  • 21
  • 90
  • 126
  • 1
    +1 Nice answer. Note that languages that optimize [tail recursion](http://en.wikipedia.org/wiki/Tail-recursive) may not throw SO on function in the question. – Alexei Levenkov Apr 17 '13 at 07:15
5

In Java:

public class Test {
    public static void main(String[] args) {
        main(args);
    }
}

In general, any recursive function that does not terminate or have too many iterations will cause this problem.

Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
NilsH
  • 13,705
  • 4
  • 41
  • 59
2

For your code snippet, this is due to recursion method call:

public string Name
{
   get{ return Name; }
}

You are calling the Name method/property recursively. The stack fills up (with the parent Name method) until it overflows and a StackOverflowException is thrown.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
2
public string Name
{
   get{ return Name; }
}

This property calling itself using with return Name;. That causes infinite recursion. That's why when you use this property, you get StackOverflowException. I believe the right usage of this property should be:

public string Name
{
   get{ return _name; }
}

StackOverflowException is thrown for execution stack overflow errors, typically in case of a very deep or unbounded recursion.

Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default. Consequently, users are advised to write their code to detect and prevent a stack overflow. For example, if your application depends on recursion, use a counter or a state condition to terminate the recursive loop.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • This answer would benefit from an explanation of why `_name` should work where `Name` doesn't. – TylerH Apr 21 '20 at 19:37