Code-Golf: What is the shortest program that compiles and crashes?

12.7k Views Asked by At

This is a little bit of fun. Can you devise the shortest program which compiles but does nothing but immediately crash when executed? Wherefore by "crash" we mean that the program stops with an error condition (a core dump for example).

Is there a language that crashes faster (7 chars) than C using a gcc compiler? [I leave this answer for somebody to find.]

(It should be allowable to use compiler flags. Otherwise 7 wouldn't work nowadays, compiler checks became much better.)

[evaluation of results] I am unable to mark a single answer because there are multiple correct ones for multiple languages. It would not be fair to disqualify one answer for another. Please use votes for choosing best answers.

35

There are 35 best solutions below

7
paxdiablo On

In C, 33 characters:

int main(void){return*((int*)0);}
5
AudioBubble On
int main () { int n = 0; return 1 / n; }
2
Ferenc Deak On

Try this in assembly:

push 0
ret

of course add the all other garbage to compile into an application.

15
spoulson On

In C, 20 characters:

void main(){main();}

Update: Suggested by roe, 15 characters:

main(){main();}

Note: Tested with VC++ 2008.

1
Naveen On

It depends upon the allowed max stack size for a thread. But it does crash when compiled using VC9:

int main()
{
    int a[10000000];
    return 0;
};
3
lc. On

In QBasic:

? 1/0

(At least I think it'll still compile and then crash with divide-by-zero; it's been quite some time...)

1
DigitalRoss On
$ cat > crash.S
hlt
$ as -o crash.o crash.S
$ ld crash.o
ld: warning: cannot find entry symbol _start; defaulting to 0000000008048054
$ ./a.out
Segmentation fault
1
Michael Lloyd Lee mlk On

How about java Z? If no file exists it will "crash" with a java.lang.NoClassDefFoundError. So my answer is zero letters. If that is not valid then...

class T{}

Would "crash" with $ java T Exception in thread "main" java.lang.NoSuchMethodError: main

If you want something that actually runs, then if you are willing to abuse things a little

class T{static {int i =1/0;}}

Else

class T{public static void main(String[]a){main(a);}}
4
Adrien Plisson On

using python:

1/0
2
Jan Willem B On

in windows powershell:

throw
8
Vilx- On

I wonder if this counts...

a

This is in JavaScript. This gives the runtime error of "object not found". Since JavaScript is a dynamic language, syntactically this is actually correct. Still feels like twisting the rules. :P

3
Aaron On

Bah - I can crash C in 5 characters:

main;

This declares an implicit int variable called 'main'. It's global so the variable has an initial value of 0. It's C the names aren't decorated - so the linker doesn't realize that it's a var and not a function.

GCC gave me a warning - but that's all.

$ gcc crash.c 
crash.c:1: warning: data definition has no type or storage class
$ ./a.exe
Segmentation fault (core dumped)
$
4
David On

F# 3 characters

1/0

Does compile, though gives a warning.

6
Brad Gilbert On

Perl

die
Died at test line 1.

die

prints the value of LIST to STDERR and exits with the current value of $! (errno).

8
Johannes Schaub - litb On

Crash with 0 characters:

$ > golf.c
$ gcc -Wl,--defsym=main=0 golf.c
$ ./a.out
Segmentation fault
2
Mark Rushakoff On

Brainf*ck

5 characters

+[>+]

It will take it a while, but eventually the program will run out of memory and inevitably crash.

5
Ether On

Perl

3 characters

1/0

Produces:

Illegal division by zero at crash.pl line 1.

(still looking for something that will do it in two..)

6
leppie On

Scheme:

(1)

Filler text to make this longer.

0
leppie On

C#, 37 chars

class F{static void Main(){Main();}}

Explodes stack.

1
AudioBubble On

X86 machine code: 1 byte

From cmd prompt in windows create file a.com containing byte F4, x86 halt instruction:

F:\>debug
-a 100
0BFD:0100 hlt
0BFD:0101
-r cx
CX 0000
:1
-n a.com
-w
Writing 00001 bytes
-q

F:\>a.com

The NTVDM CPU has encountered illegal instruction

2
gwell On

Lua, 3 characters

The divide by zero does not cause problems in Lua, but here something just as short:

a()

gives:

lua: isort.lua:1: attempt to call global 'a' (a nil value)
stack traceback:
    a.lua:1: in main chunk
    [C]: ?
0
RCIX On

to quote this answer:

All these answers and no Befunge? I'd wager a fair amount it's shortest solution of them all:

1

Not kidding. Try it yourself: http://www.quirkster.com/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.

3
Hernán On

Commodore 64 BASIC:

poke 2,2:sys2

or shorter (using PETSCII graphic-char shortcuts):

pO2,2:sY2

(crash: $02 invalid opcode on MOS/CSG6510). Actually it can be done in 7 bytes (3-instructions):

lda #$02
sta $02
jmp $0002
1
Joey Adams On

If you're at a computer store that has TI-89s, you can crash one by typing this in:

Exec "00000000"

(that's 8 zeros)

It will yield "Illegal Instruction". Press 2nd+Left+Right+ON to reset the calc.

If you want to have more fun, do this:

Exec "4E4A4E750000"

That launches the hidden hardware test menu, including memory tests, LCD tests (draws checkerboards et al.) and more. Unfortunately, the status bar gets erased, and nothing in the calc's OS draws it back, so to clean up after yourself, reset per the instructions above, or do this:

Exec "307C56E670FF20C020C020C020C020C04E750000"
0
elmattic On

F#, 11 characters

box 0 :?> unit

Compiles without a warning. Crashes with: System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'Microsoft.FSharp.Core.Unit'.

0
Brad Gilbert On

Perl in only 2 chars.

&a
Undefined subroutine &main::a called at test.pl line 1.
1
Jack V. On

Befunge, 1 character

pace RCIX, several answers from the shortest program to overflow the stack What's the shortest code to cause a stack overflow? also apply; the shortest is the befunge program:

1

PS: Golfscript, no short answer

It seems golfscript http://www.golfscript.com/ is bad at this: I assumed there'd be a one-character stack-underflow or stack-overflow, but it seems designed not to crash. Hopefully someone who knows the language better will fill in more details.

0
bendin On

Factor (or Forth)

.

"Data stack underflow"

1
Dereleased On

Late, but whatever. PHP, 32 characters.

$r=function($z){$z($z);};$r($r);

gives Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 261900 bytes) in ...

Technically, I could also do it in 29 with

$r=function($z){$z();};$r(0);

But that isn't as much fun as infinite recursion. That, and I don't think "undefined function" errors should count as "compiling" in a scripting language, otherwise:

Z();

would be the smallest solution.

Also, instead of crashing, how about (as a script) surpassing max execution time? 8 chars:

for(;;);

My original take on that had a $i++ in the third expression of the for loop, but because PHP treats all integers as signed, instead of overflowing, it just goes negative.

0
John La Rooy On

Golfscript - 1 Char

Lots of operators can do it, eg

*
(eval):1:in `initialize': undefined method `class_id' for nil:NilClass (NoMethodError)
from ../golfscript.rb:285:in `call'
from ../golfscript.rb:285:in `go'
from (eval):1:in `initialize'
from ../golfscript.rb:285:in `call'
from ../golfscript.rb:285:in `go'
from ../golfscript.rb:477
2
Rayne On
main = undefined

In Haskell.

0
Skizz On

0 Bytes

Using A86, you can have a zero sized source file!

Using the DOS command prompt:

> copy con crash.asm
[ctrl-z]
1 file(s) copied
> dir *.asm
11/12/2009  13:59                 0 crash.asm
> a86 crash.asm
A86 macro assembler, V4.05 Copyright 2000 Eric Isaacson
Source:
crash.asm
Object: crash.COM
Symbols: crash.SYM
> dir *.com
11/12/2009  13:59                 0 CRASH.COM
> c.com
0
Dónal On

Groovy 3 characters

1/0

Produces

java.lang.ArithmeticException: / by zero

0
KirarinSnow On

PostScript, 1 character

Like GolfScript:

*

Syntactically legal, but crashes during runtime because the token * is not defined (different reason than why GolfScript crashes).

0
user3599959 On

NASM

mov al, [0]

$ nasm -f elf64 -o crash.o crash.asm
$ ld -o crash crash.o
$ ./crash