78
\$\begingroup\$

Write a program that throws a StackOverflow Error or the equivalent in the language used. For example, in java, the program should throw java.lang.StackOverflowError.

You are not allowed to define a function that calls itself or a new class(except the one containing main in java). It should use the classes of the selected programming language.

And it should not throw the error explicitly.

\$\endgroup\$
9
  • 4
    \$\begingroup\$ I don't understand "use the classes of the selected programming language" \$\endgroup\$ Commented Jan 4, 2013 at 15:07
  • 3
    \$\begingroup\$ Is it ok to define a function that calls inner function like this def s{def t=s;t} ? \$\endgroup\$ Commented Jan 4, 2013 at 15:10
  • 13
    \$\begingroup\$ In most languages, classes are only a special kind of data structure, not the center of the universe. Many don't even have such a thing. \$\endgroup\$ Commented Jan 4, 2013 at 21:42
  • 1
    \$\begingroup\$ The funny thing here is that languages that require tail recursion elimination (and implementations that support it when the languages does not require it)---which are in a very real sense better---are at a disadvantage on this. TwiNight's answer links to the version of this that exists on Stack Overflow from the early days. \$\endgroup\$ Commented Jan 4, 2013 at 22:13
  • 1
    \$\begingroup\$ From the java doc: Thrown when a stack overflow occurs because an application recurses too deeply. docs.oracle.com/javase/6/docs/api/java/lang/… \$\endgroup\$
    – jsedano
    Commented Apr 9, 2013 at 16:24

68 Answers 68

1 2
3
0
\$\begingroup\$

Node.js REPL (15)

eval(RegExp.$1)

Sample session:

> eval(RegExp.$1)
RangeError: Maximum call stack size exceeded
    at eval (eval at <anonymous> (repl:1:12), <anonymous>:1:12)
    at eval (eval at <anonymous> (repl:1:12), <anonymous>:1:12)
    at eval (eval at <anonymous> (repl:1:12), <anonymous>:1:12)
    at eval (eval at <anonymous> (repl:1:12), <anonymous>:1:12)
    at eval (eval at <anonymous> (repl:1:12), <anonymous>:1:12)
    at eval (eval at <anonymous> (repl:1:12), <anonymous>:1:12)
    at eval (eval at <anonymous> (repl:1:12), <anonymous>:1:12)
    at eval (eval at <anonymous> (repl:1:12), <anonymous>:1:12)
    at eval (eval at <anonymous> (repl:1:12), <anonymous>:1:12)
    at eval (eval at <anonymous> (repl:1:12), <anonymous>:1:12)
\$\endgroup\$
0
\$\begingroup\$

C, 24 characters (no cheating)

x(){main();}main(){x();}

Compile with default cc settings and ignore warnings.

C, 10 characters (with cheat)

X{Y;}Y{X;}

compile with:

cc -DX='x()' -DY='main()'
\$\endgroup\$
1
  • \$\begingroup\$ Violates the you may not define a function that calls itself rule. main() transitively calls itself. \$\endgroup\$
    – FUZxxl
    Commented Feb 24, 2015 at 13:57
0
\$\begingroup\$

CoffeeScript/Javascript - Only in Chrome (24 chars)

Sorry for waking up an old post

This only works in Google Chrome (tested in Firefox and Chrome):

JSON.stringify a:5,->a:5

http://coffeescript.org/#try:JSON.stringify%20a%3A5%2C-%3Ea%3A5

This is not converting a circular structure to JSON. Actually, it uses the replacer function: the replacer function returns another object so the object ends up stringifying that object whose replacer also returns an object.

It gets compiled to the following JavaScript:

JSON.stringify({
  a: 5
}, function() {
  return {
    a: 5
  };
});

Tested on Chrome 39.

\$\endgroup\$
0
\$\begingroup\$

Lua, 29 bytes

This one causes a stack overflow by generating a new function every call and calling it.

l=loadstring s='l(s)()'l(s)()

Here are some other creative answers:

9 bytes:

dofile'a'

This causes a C-stack overflow. Requires you put it in a file named a.

39 bytes:

package.loaded.a=z require'a'

This causes a C-stack overflow. Requires to be put in a file named a and requires LUA_PATH=a.

\$\endgroup\$
0
\$\begingroup\$

Ruby - Proc fun - 18

a=->(b){b[b]};a[a]

Basic recursive solution. In irb ends up with:

SystemStackError: stack level too deep
\$\endgroup\$
0
\$\begingroup\$

ANTLR4 / Java, 103 Bytes

grammar P;
r:(C|N)+;
C:(('p'|'i')P);
P:(N|S)*;
N:[0-9]+;
S:[A-Za-z0-9_]*;
WS:[ \r\n]+ -> skip;

To use: Better create a new directory to put the file in, I used the common aliases shown on the antlr4 site.

antlr4 P.g4 && javac P*.java && echo 'Press Ctrl-D in 5 secs.' && grun P r -gui

Don't have any idea why this works, and I think it can be golfed much.

\$\endgroup\$
0
\$\begingroup\$

Rust, 29 bytes

fn main(){let a=[0;9000000];}

I hadn't thought it would be that easy. It actually gives segmentation fault, but given that it's rust, I'm pretty sure it's the stack that throws in the towel.

I wonder if the compiler ought to give a warning about stuff like this?

\$\endgroup\$
-1
\$\begingroup\$

Java (OpenJDK 9), 70 bytes

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

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ You are not allowed to define a function that calls itself. The exception for main was meant for creating a new class, not for recursion. \$\endgroup\$ Commented Mar 21, 2017 at 15:06
  • \$\begingroup\$ That is quite the same as the Groovy Solution. \$\endgroup\$
    – Serverfrog
    Commented Mar 21, 2017 at 15:18
  • \$\begingroup\$ Also, is it even necessary to pass main args? \$\endgroup\$ Commented Mar 21, 2017 at 15:25
  • \$\begingroup\$ I do, I pass args, the String[] which the main method receives. could name it shorter as I see it \$\endgroup\$
    – Serverfrog
    Commented Mar 22, 2017 at 9:40
1 2
3

Not the answer you're looking for? Browse other questions tagged or ask your own question.