[Question]
  [
**Closed**. This question needs [details or clarity](/help/closed-questions). It is not currently accepting answers.
 
 
---
**Want to improve this question?** Add details and clarify the problem by [editing this post](/posts/9359/edit).
Closed 6 years ago.
[Improve this question](/posts/9359/edit) 
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.
  
[Answer]
# Befunge, 1
I don't know Befunge, but...
```
1
```
[from Stack overflow code golf](https://stackoverflow.com/questions/62188/stack-overflow-code-golf)
[Answer]
### Python (2.7.3), 35 characters
```
import sys
sys.setrecursionlimit(1)
```
This operation itself succeeds, but both script and interactive will immediately throw `RuntimeError: 'maximum recursion depth exceeded'` afterward as a consequence.
Inspired by elssar's answer.
[Answer]
## Coq
```
Compute 70000.
```
`70000` is just syntactic sugar for `S (S ( ... (S O) ...))` with 70000 `S`'s. I think it's the type checker that causes the stack overflow.
Here's a warning that is printed before the command is executed:
```
Warning: Stack overflow or segmentation fault happens when working with large
numbers in nat (observed threshold may vary from 5000 to 70000 depending on
your system limits and on the command executed).
```
[Answer]
# Java - 35
```
class S{static{new S();}{new S();}}
```
[Answer]
## Javascript 24 characters
Browser dependent answer (must have access to `apply`):
```
eval.apply(0,Array(999999))
```
* `eval` was the shortest global function name that I could find (anyone know of one that is shorter?)
* `apply` allows us to convert an array into function parameters, the first parameter being the context of the function (`this`)
* `Array(999999)` will create an array with the listed length. Not sure what the maximum number of arguments is, but it's less than this, and more than `99999`
IE9: 
```
SCRIPT28: Out of stack space 
SCRIPT2343: Stack overflow at line: 20 
```
Chrome 24:
```
Uncaught RangeError: Maximum call stack size exceeded 
```
FireFox 18
```
RangeError: arguments array passed to Function.prototype.apply is too large
```
**Note** — Due to the single threaded nature of javascript, infinite loops end up locking the UI and never throwing an exception.
```
while(1);
for(;;);
```
Neither of these qualify.
**Update** — this shaves off three characters:
```
eval.apply(0,Array(1e7))
```
[Answer]
# Python 2.7 (12 chars)
```
exec('{'*99)
```
results in a «s\_push: parser stack overflow»
[Answer]
# Mathematica, 4 chars
```
x=2x
```
> 
> $RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>
> 
> 
> 
[Answer]
**Clojure, 12 chars**
```
(#(%%)#(%%))
```
Running in the repl:
```
user=> (#(%%)#(%%))
StackOverflowError   user/eval404/fn--407 (NO_SOURCE_FILE:1)
```
[Answer]
# Java - 113 chars
I think this stays within the spirit of the "no self-calling methods" rule. It doesn't do it explicitly, and it even goes through a Java language construct.
```
public class S {
    public String toString() {
        return ""+this;
    }
    public static void main(String[] a) {
        new S().toString();
    }
}
```
Condensed Version:
```
public class S{public String toString(){return ""+this;}public static void main(String[] a){new S().toString();}}
```
[Answer]
# C, 19 bytes
```
main(){int i[~0u];}
```
[Answer]
## GolfScript (8 chars)
```
{]}333*`
```
Result:
```
$ golfscript.rb overflow.gs 
golfscript.rb:246:in `initialize': stack level too deep (SystemStackError)
from /home/pjt33/bin/golfscript.rb:130:in `new'
from /home/pjt33/bin/golfscript.rb:130:in `ginspect'
from /home/pjt33/bin/golfscript.rb:130:in `ginspect'
from /home/pjt33/bin/golfscript.rb:130:in `map'
from /home/pjt33/bin/golfscript.rb:130:in `ginspect'
from /home/pjt33/bin/golfscript.rb:130:in `ginspect'
from /home/pjt33/bin/golfscript.rb:130:in `map'
from /home/pjt33/bin/golfscript.rb:130:in `ginspect'
 ... 993 levels...
from (eval):4
from /home/pjt33/bin/golfscript.rb:293:in `call'
from /home/pjt33/bin/golfscript.rb:293:in `go'
from /home/pjt33/bin/golfscript.rb:485
```
Basically this creates a heavily nested data structure and then overflows the stack when trying to turn it into a string.
[Answer]
# x86 assembly, NASM syntax, 7 bytes
```
db"Pëý"
```
"Pëý" is 50 EB FD in hexadecimal, and
```
_loop:
push eax
jmp _loop
```
in x86 assembly.
[Answer]
## **Ruby, 12**
```
eval"[]"*9e3
```
Gives
```
SystemStackError: stack level too deep
```
Presumably system-dependent, but you can add orders of magnitude by bumping the last digit up (not recommended).
Edit for explanation: Similarly to some other examples, this creates a string of `[][][]`...repeated 9000 times, then evaluates it: the rightmost `[]` is parsed as a function call to the rest, and so on. If it actually got to the beginning, it would throw an ArgumentError because `[]` is an object with a `[]` method that requires one argument, but my machine throws an error a little before the stack is over nine thousand.
[Answer]
# Rebol (11 Chars)
```
do s:[do s]
```
Yields:
```
>> do(s:[do s])    
** Internal error: stack overflow
** Where: do do do do do do do do do do do do do do do do 
do do do do do do do do do do do do do do do do do do do
do do do do do do do do do do do do do do do do do do do 
do do do do do do do do do do do do do do do do do do do
do do do do do do do do do do do do do do do do do do do
do do do do do do do do do do do do do do do do do do do
do do do do do do do do do do do do do do do do do do do
do do do do do do do do do do do do do do do do do do do
do do do do do do do do do do do do do do do do do do do
do do do do do do do do do do do do do do do do do do do
do do do do do do do do do do do do...
```
Though Rebol has functions, closures, and objects...this doesn't define any of those. It defines a data structure, which in the code-as-data paradigm can be treated as code using DO.
We can probe into the question of *"what is S"* with the REPL:
```
>> s: [do s]
== [do s]
>> type? s
== block!
>> length? s
== 2
>> type? first s
== word!
>> type? second s
== word!
```
DO never turns this into a function, it invokes the evaluator in the current environment on the structure.
[Answer]
# Casio Calculator, 11 keypresses
It's quite hard to count bytes/tokens in this "language" - I've given the number of keypresses required, excluding Shift, Alpha (the second shift key) and `=` at the end - this certainly fits into 1 byte per keypress.
Tested on the *fx-85GT PLUS* model, which is a standard, non-graphing, "non-programmable" scientific calculator. Other models will work.
Just stack up 11 cube roots:
`3√` `3√` `3√` `3√`  
`3√` `3√` `3√` `3√`  
`3√` `3√` `3√`
It doesn't even give a syntax error about the missing number under the square root.
This doesn't seem to work with square roots.
Alternatively, repeat `cos(` 31 times.
## Output
```
Stack ERROR
[AC]  :Cancel
[<][>]:Goto
```
I believe that this qualifies as a stack overflow. The stack seems to be tiny...
[Answer]
# C, 35 characters
```
main(){for(;;)*(int*)alloca(1)=0;}
```
[Answer]
# Common Lisp, 7 characters
```
#1='#1#
```
[Answer]
## Python - 11 chars
```
exec'('*999
```
---
```
>>> exec'('*999
s_push: parser stack overflow
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError
```
[Answer]
# FORTH, 13 bytes
```
BEGIN 1 AGAIN
```
overflows the value stack
[Answer]
# Postscript, 7
```
{1}loop
```
Eg.
```
$ gsnd
GPL Ghostscript 9.06 (2012-08-08)
Copyright (C) 2012 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
GS>{1}loop
Error: /stackoverflow in 1
Operand stack:
   --nostringval--
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   %loop_continue   --nostringval--   --nostringval--   false   1   %stopped_push   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   %loop_continue
Dictionary stack:
   --dict:1168/1684(ro)(G)--   --dict:0/20(G)--   --dict:77/200(L)--
Current allocation mode is local
Last OS error: No such file or directory
Current file position is 8
GS<1>
```
[Answer]
## Haskell (GHC, no optimization), 25
```
main=print$sum[1..999999]
```
[sum](http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/Data-List.html#sum) is lazy in the total. This piles up a bunch of thunks, then tries to evaluate them all at the end, resulting in a stack overflow.
[Answer]
## PHP 5.4, 33 characters
```
for($n=1e5;$n--;)$a=(object)[$a];
```
This causes a stack overflow when the nested [stdClass](http://php.net/manual/en/language.types.object.php#language.types.object.casting) objects are automatically destroyed:
```
$ gdb -q php
Reading symbols from /usr/bin/php...(no debugging symbols found)...done.
(gdb) set pagination 0
(gdb) r -nr 'for($n=1e5;$n--;)$a=(object)[$a];'
Starting program: /usr/bin/php -nr 'for($n=1e5;$n--;)$a=(object)[$a];'
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Program received signal SIGSEGV, Segmentation fault.
0x00000000006debce in zend_objects_store_del_ref_by_handle_ex ()
(gdb) bt
#0  0x00000000006debce in zend_objects_store_del_ref_by_handle_ex ()
#1  0x00000000006dee73 in zend_objects_store_del_ref ()
#2  0x00000000006a91ca in _zval_ptr_dtor ()
#3  0x00000000006c5f78 in zend_hash_destroy ()
#4  0x00000000006d909c in zend_object_std_dtor ()
#5  0x00000000006d9129 in zend_objects_free_object_storage ()
#6  0x00000000006dee53 in zend_objects_store_del_ref_by_handle_ex ()
#7  0x00000000006dee73 in zend_objects_store_del_ref ()
#8  0x00000000006a91ca in _zval_ptr_dtor ()
#9  0x00000000006c5f78 in zend_hash_destroy ()
#10 0x00000000006d909c in zend_object_std_dtor ()
#11 0x00000000006d9129 in zend_objects_free_object_storage ()
[...]
#125694 0x00000000006dee53 in zend_objects_store_del_ref_by_handle_ex ()
#125695 0x00000000006dee73 in zend_objects_store_del_ref ()
#125696 0x00000000006a91ca in _zval_ptr_dtor ()
#125697 0x00000000006c5f78 in zend_hash_destroy ()
#125698 0x00000000006d909c in zend_object_std_dtor ()
#125699 0x00000000006d9129 in zend_objects_free_object_storage ()
#125700 0x00000000006dee53 in zend_objects_store_del_ref_by_handle_ex ()
#125701 0x00000000006dee73 in zend_objects_store_del_ref ()
#125702 0x00000000006a91ca in _zval_ptr_dtor ()
#125703 0x00000000006c4945 in ?? ()
#125704 0x00000000006c6481 in zend_hash_reverse_apply ()
#125705 0x00000000006a94e1 in ?? ()
#125706 0x00000000006b80e7 in ?? ()
#125707 0x0000000000657ae5 in php_request_shutdown ()
#125708 0x0000000000761a18 in ?? ()
#125709 0x000000000042c420 in ?? ()
#125710 0x00007ffff5b6976d in __libc_start_main (main=0x42bf50, argc=3, ubp_av=0x7fffffffe738, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffe728) at libc-start.c:226
#125711 0x000000000042c4b5 in _start ()
```
[Answer]
# Q/k (16 chars)
Not sure if this is in the spirit of the challenge but I *don't think* it breaks the rules:
```
s:{f`};f:{s`};f`
```
[Answer]
A bunch in the same style:
## Python, 30
```
(lambda x:x(x))(lambda y:y(y))
```
## Javascript, 38
```
(function(x){x(x)})(function(y){y(y)})
```
## Lua, 44
```
(function(x) x(x) end)(function(y) y(y) end)
```
[Answer]
# C#: 106 86 58 46 32 28
**32**: Getters can SO your machine easy in C#:
```
public int a{get{return a;}}
```
[Answer]
# LaTeX: 8 characters
```
\end\end
```
This is the same code used in [this answer](https://codegolf.stackexchange.com/questions/21114/weirdest-way-to-produce-a-stack-overflow/21322#21322). Essentially, the `\end` macro expands itself repeatedly, resulting in a stack overflow: `TeX capacity exceeded, sorry [input stack size=5000]`. A more detailed explanation can be found [here](https://tex.stackexchange.com/questions/124423/end-end-causes-tex-capacity-exceeded).
[Answer]
# INTERCAL, 12 bytes
```
(1)DO(1)NEXT
```
## Explanation:
`NEXT` is INTERCAL's version of a subroutine call (or, at least, the closest you can get). It pushes the current position onto the `NEXT` stack and jumps to the given label.
However, if the `NEXT` stack length exceeds 80, you get what's pretty much the INTERCAL version of a stack overflow:
```
ICL123I PROGRAM HAS DISAPPEARED INTO THE BLACK LAGOON
    ON THE WAY TO 1
        CORRECT SOURCE AND RESUBNIT
```
[Try it on Ideone.](http://ideone.com/6kyruD).
[Answer]
# Mornington Crescent, ~~139~~ 133
```
Take Northern Line to Bank
Take Circle Line to Temple
Take Circle Line to Temple
Take Circle Line to Bank
Take Northern Line to Angel
```
[Answer]
# X86 assembly (AT&T), 33 characters
Note that although I'm using the label `main` as a jump target, this is *not* a recursive function.
```
.globl main
main:push $0;jmp main
```
[Answer]
# Python (17):
```
c='exec c';exec c
```
]