I'd wager a fair amount it's shortest solution of them all: 1 Not kidding. Try it yourself: quirkster.com/iano/js/befunge.html EDIT: I guess I need to explain this one. The 1 operand pushes a 1 onto Befunge's internal stack and the lack of anything else puts it in a loop under the rules of the language.
Using the interpreter provided, you will eventually--and I mean eventually--hit a point where the Javascript array that represents the Befunge stack becomes too large for the browser to reallocate. If you had a simple Befunge interpreter with a smaller and bounded stack--as is the case with most of the languages below--this program would cause a more noticeable overflow faster.
4 And it hangs Firefox to boot. Nice :) – owenmarshall Sep 16 '08 at 4:14 8 Hmm … but is this really a stack overflow or just an infinite loop? My JS interpreter did not overflow, it just went on vacation, so to speak.
– Konrad Rudolph Sep 16 '08 at 7:53 18 You.. crashed my browser and.. sent my CPU fan into overdrive. – Sam152 May 11 '09 at 15:01 6 Safari asked me if I wanted to stop the script :). – Mk12 Oct 17 '09 at 19:59 28 Here's a Befunge program that overflows faster: " It loads 79 copies of the number 32 every two times it wraps around, rather than 2 copies of the number 1.
– KirarinSnow Apr 21 '10 at 1:31.
13 Argh! I have to sleep! HALP!
:-( – Bernard Sep 16 '08 at 3:13 76 Dude. You just crashed my ... – Dan Esparza Dec 12 '08 at 22:07 12 lather, rinse, repeat – Mikeage Jan 5 '09 at 13:17 8 Can I wish for five more wishes so I can get out of this loop? – Nosredna Jun 22 '09 at 18:27 11 recursion: see recursion – Mark Jun 22 '09 at 18:52.
You could also try this in C#.net throw new StackOverflowException().
6 That's not a real stack overflow! However, I'll upvote you for originality. :-P – Chris Jester-Young Sep 15 '08 at 11:56 2 Nope, but it's the quickest way because the program doesn't have to follow the stack to error, it just simply throws an exception.Genius.
– GateKiller Sep 15 '08 at 12:13 3 It's performance optimization! I like it :) – aku Sep 15 '08 at 13:31 29 The pedant in me says it doesn't cause any stack to overflow, just throws an exception. That's like saying the quickest way to be attacked by sharks is to stand in the sea and scream "Shark attack!".
Despite this, I will up-vote it. :) – Bernard Sep 15 '08 at 3:06 18 If a stack overflows in the woods with nobody around to catch, does it throw an exception? – James M.
Sep 15 '08 at 7:53.
Nemerle: This crashes the compiler with a StackOverflowException: def o(){o()}.
48 Bonus! Meta stack overflow! – Chris Jester-Young Sep 15 '08 at 12:26 2 Definite bonus points for that.
– Wedge Sep 16 '08 at 2:12.
My current best (in x86 assembly) is: push eax jmp short $-1 which results in 3 bytes of object code (50 EB FD). For 16-bit code, this is also possible: call $ which also results in 3 bytes (E8 FD FF).
6 Counting the bytes after "compiling" (or assembling) is not code-golf. – lbrandy Sep 15 '08 at 13:38 34 The question says "... what's the shortest code to cause a stack overflow? " It doesn't specify source code, interpreted code, machine code, object code or managed code... – Anders Sandvig Sep 15 '08 at 13:42 5 @lbrandy: There are enough people who can write object code directly.
I can't do it for x86 but for a certain microprocessor I can. I'd count such code. – Joey Jul 3 '10 at 22:39.
PIC18 The PIC18 answer given by TK results in the following instructions (binary): overflow PUSH 0000 0000 0000 0101 CALL overflow 1110 1100 0000 0000 0000 0000 0000 0000 However, CALL alone will perform a stack overflow: CALL $ 1110 1100 0000 0000 0000 0000 0000 0000 Smaller, faster PIC18 But RCALL (relative call) is smaller still (not global memory, so no need for the extra 2 bytes): RCALL $ 1101 1000 0000 0000 So the smallest on the PIC18 is a single instruction, 16 bits (two bytes). This would take 2 instruction cycles per loop. At 4 clock cycles per instruction cycle you've got 8 clock cycles.
The PIC18 has a 31 level stack, so after the 32nd loop it will overflow the stack, in 256 clock cycles. At 64MHz, you would overflow the stack in 4 micro seconds and 2 bytes. PIC16F5x (even smaller and faster) However, the PIC16F5x series uses 12 bit instructions: CALL $ 1001 0000 0000 Again, two instruction cycles per loop, 4 clocks per instruction so 8 clock cycles per loop.
However, the PIC16F5x has a two level stack, so on the third loop it would overflow, in 24 instructions. At 20MHz, it would overflow in 1.2 micro seconds and 1.5 bytes. Intel 4004 The Intel 4004 has an 8 bit call subroutine instruction: CALL $ 0101 0000 For the curious that corresponds to an ascii 'P'.
With a 3 level stack that takes 24 clock cycles for a total of 32.4 micro seconds and one byte. (Unless you overclock your 4004 - come on, you know you want to. ) Which is as small as the befunge answer, but much, much faster than the befunge code running in current interpreters.
2 Very nice. :-) I can't accept two answers, so I've just +1'd your answer, and hope everyone else does the same and bump it up. :-P – Chris Jester-Young Mar 6 '09 at 19:26 3 Ah, I figured that the smallest and fastest might beat out the the smallest, but such is life.
Thanks anyway! – Adam Davis Mar 6 '09 at 22:12 1 Sweet. I started out on Z-80 assembler, and it's nice to know there's still low-level awesomeness in the world!
– Mark Harrison Aug 24 '09 at 20:04 2 Indeed, I prefer smallest and fastest...gun in the west. :-P (I just realised that I hadn't responded to your comment for over a year, so I had to say something cheeky. :-P) – Chris Jester-Young Mar 17 '10 at 5:27 1 +1, You should have simply written: P.
And then do the full story behind it. – Groo Oct 17 at 7:52.
C#: public int Foo { get { return Foo; } }.
23 lol, I did this once by accident, but it wasn't as obivous. I blame intellisense. – sieben Sep 15 '08 at 12:26 3 oh, it compiles alright, set this one in a juniors lap and watch them debug for a day looking for it, the website project, will just shut down, 503, no warning, no debug, up, down.
– DevelopingChris Sep 15 '08 at 12:42 2 Thats a good one to mess with the interns – Adam Lerman Sep 15 '08 at 16:12 4 Yes, I'll admit to also doing this, once, by accident. – Si. Sep 15 '08 at 21:28 2 I started naming private variables _foo years ago because intellisense tends to cause this if your backing private field is just a lower case version of the property.
Automatic variables in C# 3 eliminate this drudgery altogether. – Brian Reiter Sep 15 '08 at 14:31.
Hoot overflow! // v___v let rec f o = f(o);(o) // '---'.
18 +1 for the lulz – eyelidlessness May 25 '10 at 1:26.
Every task needs the right tool. Meet the SO Overflow language, optimized to produce stack overflows: so.
P – Chris Jester-Young Sep 16 '08 at 10:55 5 Yea … unfortunately, I wrote it (well, the HTML page took longer than the “interpreterâ€?) before seeing the Befunge solution. Can't beat that. ;-) – Konrad Rudolph Sep 16 '08 at 12:55 6 If you're making a specialized language for generating overflows with a minimal of code, obviously you would want (1) empty input produces stack overflowing code (probably a small binary that runs the native code generated from the assembly code entry) or (2) all input programs produce said binary.
– Jared Updike Sep 18 '08 at 19:08 9 +1, although I hesitated when I discovered the language is case-insensitive. – avakar Jan 29 '10 at 23:46 15 I got a job once at a place where I'm sure this was what powered their server backend. – Rich Bradshaw Feb 27 '10 at 18:59.
TeX: \def~{~. }~ Results in:! TeX capacity exceeded, sorry input stack size=5000.
~->~ . ~->~ . ~->~ .
~->~ . ~->~ . ~->~ .... \def~{~.
}~ LaTeX: \end\end Results in:! TeX capacity exceeded, sorry input stack size=5000. \end #1->\csname end#1 \endcsname \@checkend {#1}\expandafter \endgroup \if@e... \end\end.
Z-80 assembler -- at memory location 0x0000: rst 00 one byte -- 0xC7 -- endless loop of pushing the current PC to the stack and jumping to address 0x0000.
5 +1 because I actually did that once… >. The address bus would cycle through the 64K space as the stack overflowed repeatedly, interspersed with reads of 0xff from 0x0038. – Bill Forster Feb 27 '09 at 21:22.
31 Any sensible human brain will tail-call optimise the interpretation of this one too, and not blow up. :-P – Chris Jester-Young Sep 15 '08 at 11:53 72 Chris, sensible human brains are becoming a rarity these days. – Jason Z Sep 15 '08 at 13:46 19 rarity...you mean they exist?
– Adam Lerman Sep 15 '08 at 16:13 11 Google recursion – CodeFusionMobile Sep 15 '08 at 18:46.
4 You could even be shorted by skipping the parenthesis (but including space in place of the first). – alex Sep 27 '10 at 5:14.
How about the following in BASIC: 10 GOSUB 10 (I don't have a BASIC interpreter I'm afraid so that's a guess).
3 Not really a stack overflow since BASIC is a stackless language. Even VB (which does have a stack) wouldn't overflow on this since it's just jumping, not creating a stack frame. – Daniel Spiewak Sep 16 '08 at 1:16 21 That's a GOSUB, not a GOTO.
Since it RETURNs to where it was called from, surely it's using a stack? – Tom Sep 16 '08 at 1:55 2 Yeah, I agree. I had many stack overflows in BASIC in the 80s.
– nickd Sep 16 '08 at 11:14 6 I ran this one in yabasic just for the fun of it, and it nearly took down my computer. Thank god malloc eventually failed, but I was paging like no tomorrow. – Adam Rosenfield Sep 16 '08 at 5:08 1 Oops sorry Adam... reminds me of a time at uni when someone accidentally wrote a program that recursively forked: took down an entire Silicon Graphics server.
– stusmith Sep 16 '08 at 15:34.
I loved Cody's answer heaps, so here is my similar contribution, in C++: template class Overflow { typedef typename Overflow::type type; }; typedef Overflow::type Kaboom; Not a code golf entry by any means, but still, anything for a meta stack overflow! :-P.
Here's my C contribution, weighing in at 18 characters: void o(){o();o();} This is a lot harder to tail-call optimise! :-P.
– Dinah Jun 22 '09 at 19:14 3 @Dinah: One of the constraints of my contest was that tail-call optimisation doesn't count as recursion; it's just an iterative loop. If you only wrote o() once, that can be tail-call optimised into something like this (by a competent compiler): "o: jmp o". With 2 calls of o, the compiler has to use something like: "o: call o; jmp o".
It's the recursive "call" instruction that makes the stack overflow. – Chris Jester-Young Jun 22 '09 at 19:30.
Using a Window's batch file named "s. Bat": call s.
Javascript To trim a few more characters, and to get ourselves kicked out of more software shops, let's go with: eval(i='eval(i)').
2 Hehehe, very neat! Mmmm, eval.... – Chris Jester-Young Sep 15 '08 at 23:13.
Please tell me what the acronym "GNU" stands for.
4 Or Eine (Eine is not Emacs), or Zwei (Zwei was Eine initially). :-P – Chris Jester-Young Jun 22 '09 at 19:22.
Groovy: main() $ groovy stack. Groovy: Caught: java.lang. StackOverflowError at stack.
Main(stack. Groovy) at stack. Run(stack.
Groovy:1) ...
Person JeffAtwood; Person JoelSpolsky; JeffAtwood. TalkTo(JoelSpolsky); Here's hoping for no tail recursion!
1 Hehe, funny. Related to conversations, the idea of the "echo chamber effect" is quite interesting, too. Not quite stack overflow-inducing, but still.
– Chris Jester-Young Sep 17 '08 at 0:13 8 Wouldn't this be a null pointer exception? Sorry, I know it's a joke. – jamesh Oct 23 '08 at 12:11.
C - It's not the shortest, but it's recursion-free. It's also not portable: it crashes on Solaris, but some alloca() implementations might return an error here (or call malloc()). The call to printf() is necessary.
#include #include #include int main(int argc, char *argv) { struct rlimit rl = {0}; getrlimit(RLIMIT_STACK, &rl); (void) alloca(rl. Rlim_cur); printf("Goodbye, world\n"); return 0; }.
Perl in 12 chars: $_=sub{&$_};&$_ bash in 10 chars (the space in the function is important): i(){ i;};i.
Try and put more than 4 patties on a single burger. Stack overflow.
1 Here in New Zealand we have Burger Wisconsin, where they use big but thin patties. I'm sure you can stack more than 4 of those if you want to; it'll be a very expensive burger though! – Chris Jester-Young Sep 15 '08 at 23:09.
Python: so=lambda:so();so() Alternatively: def so():so() so() And if Python optimized tail calls...: o=lambda:map(o,o());o().
I'm selecting the “best answer†after this post. But first, I'd like to acknowledge some very original contributions: aku's ones. Each one explores a new and original way of causing stack overflow.
The idea of doing f(x) ⇒ f(f(x)) is one I'll explore in my next entry, below. :-) Cody's one that gave the Nemerle compiler a stack overflow. And (a bit grudgingly), GateKiller's one about throwing a stack overflow exception.
:-P Much as I love the above, the challenge is about doing code golf, and to be fair to respondents, I have to award “best answer†to the shortest code, which is the Befunge entry; I don't believe anybody will be able to beat that (although Konrad has certainly tried), so congrats Patrick! Seeing the large number of stack-overflow-by-recursion solutions, I'm surprised that nobody has (as of current writing) brought up the Y combinator (see Dick Gabriel's essay, The Why of Y, for a primer). I have a recursive solution that uses the Y combinator, as well as aku's f(f(x)) approach.
:-) ((Y (lambda (f) (lambda (x) (f (f x))))) #f).
Here's another interesting one from Scheme: ((lambda (x) (x x)) (lambda (x) (x x))).
Java Slightly shorter version of the Java solution. Class X{public static void main(Stringa){main(a);}}.
2 Stack underflow for the win! – Chris Jester-Young Sep 15 '08 at 23:06.
Intel(?) documentation, this is also 3 bytes: label: call label.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.