Alex Rivera | Logout

What's the shortest code to cause a stack overflow?

Asked 2008-09-15T11:17:38.467
160

To commemorate the public launch of Stack Overflow, what's the shortest code to cause a stack overflow? Any language welcome.

ETA: Just to be clear on this question, seeing as I'm an occasional Scheme user: tail-call "recursion" is really iteration, and any solution which can be converted to an iterative solution relatively trivially by a decent compiler won't be counted. :-P

ETA2: I've now selected a “best answer”; see this post for rationale. Thanks to everyone who contributed! :-)

Edit
Report

17 Answers

77

C#:

public int Foo { get { return Foo; } }
answered 2008-09-15T11:21:28.457
26

How about the following in BASIC:

10 GOSUB 10

(I don't have a BASIC interpreter I'm afraid so that's a guess).

answered 2008-09-15T12:24:45.933
3

GWBASIC output...

OK
10 i=0
20 print i;
30 i=i+1
40 gosub 20
run
 0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21
 22  23  24  25  26  27  28  29  30  31  32  33
Out of memory in 30
Ok

Not much stack depth there :-)

answered 2008-09-15T13:47:40.820
3

batch program called call.cmd;

call call.cmd

******  B A T C H   R E C U R S I O N  exceeds STACK limits ******
Recursion Count=1240, Stack Usage=90 percent
******       B A T C H   PROCESSING IS   A B O R T E D      ******
answered 2008-09-15T14:16:38.253
2

PIC18:

overflow

    PUSH   
    CALL   overflow 
answered 2008-09-15T11:34:39.190
2

In Whitespace, I think:

It probably won't show up. :/

answered 2008-09-15T14:53:39.633
2

Unless there's a language where the empty program causes a stack overflow, the following should be the shortest possible.

Befunge:

:

Duplicates the top stack value over and over again.

edit: Patrick's is better. Filling the stack with 1s is better than filling the stack with 0s, since the interpreter could optimize pushing 0s onto an empty stack as a no-op.

answered 2008-09-15T22:31:08.440
2

Java:

class X{static{new X();}{new X();}}

Actually causes a stack overflow initializing the X class. Before main() is called, the JVM must load the class, and when it does so it triggers any anonymous static code blocks:

static {
  new X();
}

Which as you can see, instantiates X using the default constructor. The JVM will call anonymous code blocks even before the constructor:

{
  new X();
}

Which is the recursive part.

answered 2010-02-27T18:48:56.477
2

Java: 35 characters

I think it's too late, but I will still post my idea:

class A{{new A();}static{new A();}}

Using the static initializer and instance initializer features.

Here is the output on my computer (notice it showed two error messages):

Exception in thread "main" java.lang.StackOverflowError
    at A.<init>(A.java:1)
        ......
    at A.<init>(A.java:1)
Could not find the main class: A. Program will exit.

See also: http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/java/javaOO/initial.html

answered 2010-07-17T19:03:00.830
0

Clarion:

Poke(0)
answered 2008-09-15T12:41:28.373
0

On the cell spus, there are no stack overflows, so theres no need for recursion, we can just wipe the stack pointer.

asm("andi $1, $1, 0" );

answered 2008-09-15T13:19:41.777
0

PHP - recursion just for fun. I imagine needing a PHP interpreter takes it out of the running, but hey - it'll make the crash.

function a() { a(); } a();
answered 2008-09-15T13:35:37.837
0

bash: Only one process

\#!/bin/bash
of() { of; }
of
answered 2008-09-15T16:09:44.910
0

In a PostScript file called so.ps will cause execstackoverflow

%!PS
/increase {1 add} def
1 increase
(so.ps) run
answered 2008-09-16T05:37:25.140
0

Prolog

This program crashes both SWI-Prolog and Sicstus Prolog when consulted.

p :- p, q.
:- p.
answered 2009-02-24T21:21:54.810
0

Meta problem in D:

class C(int i) { C!(i+1) c; }
C!(1) c;

compile time stack overflow

answered 2009-04-22T20:36:09.433
-1

Ruby:

def i()i()end;i()

(17 chars)

answered 2008-09-15T14:01:40.647

Your Answer