Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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.

share|improve this question
3  
I don't understand "use the classes of the selected programming language" –  Prince John Wesley Jan 4 '13 at 15:07
2  
Is it ok to define a function that calls inner function like this def s{def t=s;t} ? –  Prince John Wesley Jan 4 '13 at 15:10
8  
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. –  leftaroundabout Jan 4 '13 at 21:42
1  
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. –  dmckee Jan 4 '13 at 22:13
1  
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/… –  jsedano Apr 9 '13 at 16:24

55 Answers 55

Befunge, 1

I don't know Befunge, but...

1

from Stack overflow code golf

share|improve this answer
13  
Explanation: 1 is a numeric literal that gets pushed to the stack when encountered. In Befunge, control flow wraps around until it encounters an @ to end the program. –  histocrat Jan 4 '13 at 17:30
3  
I didn't know there was this question on StackOverflow. I searched only on this site before posting. –  True Soft Jan 5 '13 at 8:26
18  
I'm mildly flattered to see my answer here. –  Patrick Apr 9 '13 at 1:50
    
This works in ><> too. –  Cruncher Nov 6 '13 at 15:09

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.

share|improve this answer
    
I thought about putting that up as my solution instead, but wasn't sure if the error could be considered a stack overflow. Though, essentially, that is what it is, right? –  elssar Jan 5 '13 at 8:25

This is a troll answer, slightly inspired by the warning in this answer.

I'm noting that the question asks for a StackOverflow error, not a stack overflow error. Interpreting this very literally, you are actually supposed to create an error from the site StackOverflow. The shortest way to do this is by requesting a page that doesn't exist, and voilà, you have a StackOverflow 404 error.

$ wget stackoverflow.com/x
--2014-01-04 18:56:16--  http://stackoverflow.com/x
Resolving stackoverflow.com (stackoverflow.com)... 198.252.206.16
Connecting to stackoverflow.com (stackoverflow.com)|198.252.206.16|:80... connected.
HTTP request sent, awaiting response... 404 Not Found
2014-01-04 18:56:16 ERROR 404: Not Found.
share|improve this answer
    
Brillant! Ridiculous! –  quetzalcoatl 2 days ago

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))
share|improve this answer
    
MDN says that eval is the shortest. –  Peter Taylor Jan 23 '13 at 13:30
    
eval.apply(0,Array(1e6)) saves 3 chars, you can even go with 9e9 at no cost –  plg Feb 2 at 22:30
    
apply is a standard ECMAScript feature. There is nothing browser dependent. Unless you are talking about really old browsers, but this wouldn't work in hypothetical Netscape 2 with apply anyway, because Array class doesn't exist in Netscape 2. –  xfix Apr 2 at 12:02

Python 2.7 (12 chars)

exec('{'*99)

results in a «s_push: parser stack overflow»

share|improve this answer
3  
I get SyntaxError: unexpected EOF while parsing –  moose Jan 14 '13 at 7:15
1  
With exec('{'*101) I get MemoryError –  moose Jan 14 '13 at 7:15
2  
In Python2, exec is a statement, so you can just use exec'{'*999 (99 doesn't seem to be enough) –  gnibbler Feb 3 at 1:15

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.

share|improve this answer
    
For me, this doesn't throw an error, but outputs [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[‌​[ [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[[[[[[[""]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] ]]]]]]]]]]]]]]]]] (and so on, output too long for comments) –  ProgramFOX Feb 2 at 16:30
    
@ProgramFOX, there will be some value which you can replace 333 with and it will break. 333 was the smallest value which broke for me, but if you have a different version of Ruby (or maybe the same version on a different OS, for all I know) it might handle a different number of stack frames before overflowing. –  Peter Taylor Feb 2 at 22:54
    
Yes, if I replace it with 999 it works fine. –  ProgramFOX Feb 3 at 17:41
    
Breaks at 3192 on my machine, so 6.? still works without adding characters. –  Dennis Jun 26 at 22:53

Clojure, 12 chars

(#(%%)#(%%))

Running in the repl:

user=> (#(%%)#(%%))
StackOverflowError   user/eval404/fn--407 (NO_SOURCE_FILE:1)
share|improve this 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).
share|improve this answer
1  
That might let you think Coq is an incredibly dumb language... funny... –  leftaroundabout Jan 6 '13 at 13:01
1  
@leftaroundabout Actually not. The Nat type is a type level peano numeral that must act as if it is a linked list. –  FUZxxl Jan 6 '13 at 15:40
1  
@FUZxxl: my comment was not not meant ironically at all. Decide for yourself if you want to include classical logic into that sentence, or prefer to stay constructive... –  leftaroundabout Jan 6 '13 at 20:42
2  
@leftaroundabout Oops... sorry. I forgot that the markdown parser always eats those nice &lt;irony&gt;-tags. –  FUZxxl Jan 6 '13 at 21:28

Java - 35

class S{static{new S();}{new S();}}
share|improve this answer
    
Didn't OP say no new classes? I don't see a public static void main in there. Or am I just failing to understand Java? –  B1KMusic Feb 5 at 19:17
    
@B1KMusic There are no new classes, there's only one class (S). The code uses a static initializer, it throws the SO before the jvm figures out there's no main method. Works with java 6. –  aditsu Feb 5 at 20:20
    
Thanks for clearing that up. –  B1KMusic Feb 7 at 23:15
    
I understand the static block. But what is the next block ? –  Nicolas Barbulesco Jun 7 at 20:51
    
@NicolasBarbulesco That's an initializer block, it's executed when you construct a new instance. –  aditsu Jun 8 at 6:41

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();}}
share|improve this answer
7  
Well, ""+this is actually ""+this.toString(), so the method calls itself. –  True Soft Jan 19 '13 at 8:08
    
@TrueSoft Pretty sure java throws in a StringBuilder object there. toString will likely be called from within there. –  Cruncher Feb 18 at 21:48
    
By the time the compiler and optimizer are done, the toString() method ends up being public java.lang.String toString() { return this.toString(); } –  Jonathan Callen Feb 23 at 19:38

C, 35 characters

main(){for(;;)*(int*)alloca(1)=0;}
share|improve this answer
    
Why store anything in the assigned space? –  Peter Taylor Jan 5 '13 at 17:15
1  
In this case, it's impossible to solve this problem in C. –  FUZxxl Jan 6 '13 at 15:41
2  
@dmckee, Not all segmentation faults are stack overflows, but I'd say this one is, since it's the result of exceeding the stack capacity. –  ugoren Jan 6 '13 at 19:12
1  
@dmckee, alloca allocates from the stack. –  ugoren Jan 6 '13 at 19:15
1  
@PeterTaylor: It probably depends on the implementation but in my case alloca(1) is basically translated to sub $1, %esp so the stack isn't touched. –  Job Jan 7 '13 at 14:48

FORTH (13)

BEGIN 1 AGAIN

overflows the value stack

share|improve this answer
    
: X X ; X (9) must overflow return stack –  AMK Jan 18 '13 at 14:45
    
won't work (X isn't defined while defining the call and that's a self reference/recursion –  ratchet freak Jan 18 '13 at 14:50
    
@ratchetfreak, those control words can only be used in a compile state, so they need to be wrapped in a :...; word definition. That adds at least 6 characters, plus at least 2 more for this to execute as a program. You might be able to do it shorter, but here's an example: : F BEGIN 1 AGAIN ; F. I suggest this because the question asks: "Write a program." Anyways, gave you an upvote for Forth, regardless of char count! :-) –  Darren Stone Jan 4 at 18:08

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>
share|improve this answer

Haskell (GHC, no optimization), 25

main=print$sum[1..999999]

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.

share|improve this answer
    
i always thought sum was implemented using foldl'. isn't it? –  proud haskeller Jul 30 at 19:25
    

Mathematica, 4 chars

x=2x

$RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>

share|improve this answer
    
"You may not define function that calls itself" –  Tomas Feb 2 at 16:03
3  
That isn't a function, it's a variable (unless it isn't at all what it looks like). –  A.M.K Feb 3 at 3:12
    
You took my idea. –  PyRulez Apr 16 at 21:09

C: 19 characters

main(){int i[~0u];}
share|improve this answer
3  
@Thomas Yes it is a stack overflow on any machine where local variables are allocated on the stack. Since the C language has no concept of a stack overflow indication (it's all undefined behavior; one of them manifests itself as a segfault), this does fit the original requirement. –  Jens Feb 2 at 17:45
    
OK, sorry, accepted. –  Tomas Feb 2 at 17:49
2  
it gives main.c:1:16: error: size of array 'i' is negative for me on gcc 4.8.1. The unsigned version main(){int i[~0U];} works. –  Csq Feb 25 at 16:00
    
@Csq Corrected, thanks! –  Jens Feb 26 at 9:59

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.

share|improve this answer
    
hmm... crashed IRB :P –  Doorknob Mar 22 '13 at 23:35
    
Which version? ruby 1.9.2 throws “ArgumentError: wrong number of arguments (0 for 1..2)”. –  manatwork Jun 1 '13 at 11:24
    
Found an old ruby 1.8.7. There the posted code works as described. –  manatwork Jun 1 '13 at 11:58
    
Odd, it works on my 1.8.7, 1.9.2, and 1.9.3. –  histocrat Jun 3 '13 at 12:24

PHP 5.4, 33 characters

for($n=1e5;$n--;)$a=(object)[$a];

This causes a stack overflow when the nested stdClass 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 ()
share|improve this answer
    
+1 for what must be PHP's second appearance on CodeGolf! –  Bojangles Apr 11 '13 at 13:53

C#: 106 86 58 46 32 28

32: Getters can SO your machine easy in C#:

public int a{get{return a;}}
share|improve this answer
1  
No need for setter public int a {get{return a;}} –  Mika Kolari Apr 10 '13 at 10:35
3  
This violates the rule "You are not allowed to define a function which calls itself". Admittedly it's hidden behind syntax sugar, but it's still missing the point of the challenge. –  Peter Taylor Apr 11 '13 at 13:08
    
Adding the setter somewhat circumvents the rule, because you now have two functions calling each other. But I wonder: does that still violate the OP's intentions behind this challenge? –  Andrew Gray Apr 11 '13 at 13:10
    
The idea as I understand it is to find some excessively nested recursion in the interpreter or standard API of the language. This might not be too easy in C#. –  Peter Taylor Apr 13 '13 at 21:21
1  
Why "public string"? "int" works just as well: int a { get { return a; } } –  NPSF3000 Nov 9 '13 at 11:16

x86 assembly, NASM syntax, 7 bytes

db"Pëý"

"Pëý" is 50 EB FD in hexadecimal, and

_loop:
push eax
jmp _loop

in x86 assembly.

share|improve this 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
share|improve this answer
    
Nice idea: this is a sort of recursion-without-recursion! –  Andrea Corbellini Jan 9 '13 at 21:29
    
using a86: dd 0fdeb60 10 characters! –  Skizz Jan 15 '13 at 16:17

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`
share|improve this answer
    
It's a shame C# requires so much typing, you inspired my answer! –  Andrew Gray Apr 8 '13 at 18:59

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)
share|improve this answer
    
In Python x=lambda y:y(y);x(x) is shorter (20 chars). This function is not recursive. x calls any function passed to it as an argument. –  AMK Apr 17 '13 at 16:28
    
Ruby 2.0 - ->x{x[x]}[->y{y[y]}] –  Jan Dvorak Nov 1 '13 at 12:10
    
Mathematica #@#&[#@#&] –  alephalpha Nov 13 '13 at 14:57
    
You're just using recursion, then why not do just that, for example in JS: (function x(){x()})() ? –  xem Jan 1 at 14:29
    
@xem Requirements say no recursion, that is why. –  Danny Feb 14 at 13:16
//Java's answer...

public class Test
{
    private Test test = new Test();

    public static void main(String[] args)
    {
        new Test();
    }
}
share|improve this answer

Groovy (27 chars)

a=[:];b=[a:a];a.b=b;print b

And so it goes:

Caught: java.lang.StackOverflowError
    java.lang.StackOverflowError
share|improve this answer

Python, real stack overflow: 38

a=[];eval("[x "+"for x in a "*800+"]")

Error message:

s_push: parser stack overflow
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError

Explanation:

[x for x in a for x in a]

is the same as

y = []
for x in a:
    for x in a:
        y.append(x)

so the above eval produces 800 nested for loops :)

share|improve this answer
    
Ah just noticed that someone else already exploited the parser in a similar, more elegant way: codegolf.stackexchange.com/a/9370/6491 –  stefreak Mar 29 '13 at 13:03

Tcl, 15 chars

interp r {} 1;a

gives

too many nested evaluations (infinite loop?)

the interp r stands for interp recursionlimit (you can abbreviate subcommands). a calls (because not known) unknown, which calls a lot of other stuff.

share|improve this answer

Common Lisp, 7 characters

#1='#1#
share|improve this answer
    
Beautiful...I was planning to use #1=(#1#) for the terminal and (print #1=(#1#)), but your solution is so much better. –  protist Nov 19 '13 at 16:29
    
Actually that doesn't overflow at read time, only when you attempt to print it. So aside from the 1 character difference, yours is no better. –  protist Nov 19 '13 at 16:31
    
You're right, just edited that out. I'm not sure if there's a way to cause an overflow at read-time. –  Erik Haliewicz Jan 11 at 22:04
    
Actually, #.#1='#1# causes a read-time overflow :-) –  Erik Haliewicz Apr 27 at 22:26

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.

share|improve this answer
1  
+1 ... I hadn't noticed that my answer was defining a function and that was against the rules, but edited my answer to use DO...then noticed you'd already submitted that answer. So I just deleted mine, but since I'd written up why this isn't defining an object/function/closure I thought I'd put the explanation into yours. Also I think the do do do do is kind of funny and worth including. :-) Hope that's ok! –  Dr. Rebmu Apr 16 at 20:44

C -- 34 characters, no libraries

Though competative with Job's answer,

o(){O();}
O(){o();o();}
main(){o();}

violates the spirt of the challenge by showing how to evade the restriction on constructing a simple recursion. You can save 4 character by removing one call from O, but gcc is smart enough to recongnise that if you use -O3.1

The trick is quite general and can be done in fortran 77, too (142 characters):

      program o
      i=j()
      stop
      end
      function j()
      j=k()
      end
      function k()
      k=j()
      k=j()
      end

Again, gcc can optimize away a single call in each.


1 I suppose it inlines one of them and then applys tail recursion elimination. How cool is that!?!.

share|improve this answer
    
O(){o(o());} saves a character. Replacing O with recursive main call saves more. –  ugoren Jan 15 '13 at 7:43

protected by professorfish Oct 9 at 17:48

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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