76
\$\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\$
  • 4
    \$\begingroup\$ I don't understand "use the classes of the selected programming language" \$\endgroup\$ – Prince John Wesley Jan 4 '13 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\$ – Prince John Wesley Jan 4 '13 at 15:10
  • 12
    \$\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\$ – ceased to turn counterclockwis Jan 4 '13 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\$ – dmckee --- ex-moderator kitten Jan 4 '13 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 Apr 9 '13 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\$
  • \$\begingroup\$ Violates the you may not define a function that calls itself rule. main() transitively calls itself. \$\endgroup\$ – FUZxxl Feb 24 '15 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\$
  • \$\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\$ – fəˈnɛtɪk Mar 21 '17 at 15:06
  • \$\begingroup\$ That is quite the same as the Groovy Solution. \$\endgroup\$ – Serverfrog Mar 21 '17 at 15:18
  • \$\begingroup\$ Also, is it even necessary to pass main args? \$\endgroup\$ – fəˈnɛtɪk Mar 21 '17 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 Mar 22 '17 at 9:40
1 2 3

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