41

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.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Don Johe
  • 989
  • 2
  • 12
  • 24

35 Answers35

127

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)
$
Aaron
  • 9,123
  • 5
  • 40
  • 38
66

Crash with 0 characters:

$ > golf.c
$ gcc -Wl,--defsym=main=0 golf.c
$ ./a.out
Segmentation fault
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • and me (temporarily) without voting capabilities – rcollyer Nov 20 '09 at 21:47
  • 20
    Pssh, using special compiler flags should be included in the character count. =] – strager Nov 20 '09 at 23:06
  • +1 for this one. I've *never* seen a code golf done in 0 characters (even a Perl one). – paxdiablo Nov 21 '09 at 00:23
  • 2
    and sadly, you still haven't: command line switches count against your strokes. :) – Robert P Nov 21 '09 at 00:59
  • 4
    I did the post as an example that command line can do everything one wants (including making a shell command that appends the code into an empty file, and declaring that shell command as the compiler). There should be a more precise rule set for this question, i think. – Johannes Schaub - litb Nov 21 '09 at 01:57
  • 1
    @paxdiablo: I think an empty program was submitted to the obfuscated C contest as the shortest self-outputting program in some language. Hilarity ensued. But I may be conflated separate incidents. – Jack V. Nov 23 '09 at 17:17
  • Sticking to the question I absolutely allowed for arbitrary compiler flags and never counted them towards the used characters. (People think harder before posting funny questions *g*.) – Don Johe Nov 24 '09 at 09:05
  • Jack: It was IOCCC and that 'some language' was, I believe, C. –  Jan 03 '10 at 08:03
49

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

Vilx-
  • 104,512
  • 87
  • 279
  • 422
  • is it actually a valid statement? won't it at least need a semicolon or something? – falstro Nov 20 '09 at 14:00
  • 4
    @roe: IIRC, semicolons are used as separators between statements in Javascript, so you don't need one after the last statement. – Guffa Nov 20 '09 at 14:07
  • 1
    I should have distinguished between compiler languages and script languages, but then we'd argued about what is to be considered a script language. (This works in python as well.) – Don Johe Nov 20 '09 at 14:19
  • 1
    @roe yes this is a valid statement because javascript will perform semicolon insertion. Also, just like C, a pointless evaluation statement is allowed in javascript, `someVar;` doesn't really do anything useful, but the variable gets evaluated and it is part of the spec. – TM. Nov 20 '09 at 14:34
  • generalized this might be called a general solution for object oriented scripting languages with dynamic types. – Don Johe Nov 20 '09 at 15:54
  • To get this to fail in Perl, you would either need to turn on strict, or add parentheses. `a()` – Brad Gilbert Nov 20 '09 at 16:54
  • 3
    Actually, semicolons are completely optional in JavaScript. Try it yourself! And yes - I suppose this might work in a whole range of dynamic scripting languages. I'm just not familiar with any others. – Vilx- Nov 20 '09 at 20:45
  • Vilx: exactly. actually leaving out all semicolons except in for loops works well in JavaScript as long as you don’t call anonymous functions like `(function{…})()`, because if something semicolonless is before the first brace, javascript will call that with the function as argument. so prefix braces or brackets which start their lines with semicolons, and leave out all others. – flying sheep May 24 '13 at 23:14
47

using python:

1/0
Adrien Plisson
  • 22,486
  • 6
  • 42
  • 73
33

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

  • DOS's "*.COM" format requires zero overhead, but UNIX generally uses ELF for executables, which is similar in concept Windows's PE: headers and sections, and rather impossible to get below 45 bytes (http://www.muppetlabs.com/~breadbox/software/tiny/) – ephemient Nov 24 '09 at 16:31
24
$ 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
DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
23

Perl

die
Died at test line 1.

die

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

Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
11

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
Hernán
  • 4,527
  • 2
  • 32
  • 47
11

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"
Joey Adams
  • 41,996
  • 18
  • 86
  • 115
10

Brainf*ck

5 characters

+[>+]

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

Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
8

Factor (or Forth)

.

"Data stack underflow"

bendin
  • 9,424
  • 1
  • 39
  • 37
7

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);}}
Michael Lloyd Lee mlk
  • 14,561
  • 3
  • 44
  • 81
7

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.

Community
  • 1
  • 1
RCIX
  • 38,647
  • 50
  • 150
  • 207
6

In C, 20 characters:

void main(){main();}

Update: Suggested by roe, 15 characters:

main(){main();}

Note: Tested with VC++ 2008.

spoulson
  • 21,335
  • 15
  • 77
  • 102
  • 2
    Not valid according to the standard :-) – paxdiablo Nov 20 '09 at 13:38
  • 4
    "`main(){return main();}`" is though. – falstro Nov 20 '09 at 13:42
  • 1
    Actually it should crash after a lot of recursions. And thus "work" according to the question. – Don Johe Nov 20 '09 at 13:44
  • so is "`main(){main();}`" I guess, it'll give a warning about reaching end of non-void function though. – falstro Nov 20 '09 at 13:44
  • @roe: no, it's not. You need to read the standard, if you have a spare two weeks and are feeling masochistic :-) 'int main(int,char*[])' and 'int main(void)' are the only two guaranteed forms. – paxdiablo Nov 20 '09 at 13:44
  • @paxdiablo: int is a default return type, so omitting it is ok, void is also the default parameter type, so it too may be omitted. – falstro Nov 20 '09 at 13:45
  • Yes, but standard compliance is not a requirement here. It compiles and crashes in VC++ 2008. – spoulson Nov 20 '09 at 13:48
  • @roe, I really hate to be pedantic (well, I don't hate it that much) but any main prototype other than the two I mentioned is into implementation-defined behavior. See s5.1.2.2.1 of the standard. – paxdiablo Nov 20 '09 at 13:50
  • @paxdiablo: nothing wrong with being pedantic but there are earlier standards of the C language which have different requirements. – Konrad Rudolph Nov 20 '09 at 13:55
  • 2
    @paxdiablo: there isn't a 'the standard' there's a C99 standard and a C89 standard (and a bunch of others as well). `x(){...}` is perfectly valid for `int x(void) {...}` in earlier C standards. So it's valid C under that standard. – falstro Nov 20 '09 at 13:56
  • Under C89 Standard it is invalid to omit the `return` statement in `main()`, so it's invalid again :) – pmg Nov 20 '09 at 15:06
  • @pmg: rats! ;) I was kind of expecting someone to point that out... I wasn't sure myself, only knew it'd produce a warning. – falstro Nov 20 '09 at 15:22
  • On the other side, the function will never come to exit, so it may leave out the `return`, i think. – Johannes Schaub - litb Nov 20 '09 at 16:21
  • 6
    So, even if it isn't valid according to the standard, what's the problem? It *compiles* and *crashes*. – R. Martinho Fernandes Nov 22 '09 at 00:37
  • People actually compile code designed to crash, instantly, _with warnings enabled_ ? – Tim Post Apr 24 '10 at 05:53
5

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.

Dereleased
  • 9,939
  • 3
  • 35
  • 51
4

Try this in assembly:

push 0
ret

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

Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
  • As you probably could build an assembler that compiles this garbage in automatically this might be valid. But I actually don't know if such an assembler exists. – Don Johe Nov 20 '09 at 13:52
  • a86 was just such a beast - needed none of that gumpff that plagued masm. – paxdiablo Nov 20 '09 at 13:58
4

in windows powershell:

throw
Jan Willem B
  • 3,787
  • 1
  • 25
  • 39
  • 6
    The 1/0 trick does it in powershell as well. No reason to waste two innocent characters ;-) – Don Johe Nov 20 '09 at 15:47
  • 2
    :) (I just wanted to insert this smiley but SO requires comments to be at least 15 characters long, so that is 158 more innocent characters wasted. I am sorry.) – Jan Willem B Nov 21 '09 at 22:57
4

Scheme:

(1)

Filler text to make this longer.

dfeuer
  • 48,079
  • 5
  • 63
  • 167
leppie
  • 115,091
  • 17
  • 196
  • 297
  • I know I'm a little late, but would this actually compile? – dfeuer May 22 '14 at 02:00
  • Answer: no. Racket and Chicken (at least) refuse to build an executable from that "program". Chicken says "Syntax error: illegal atomic form". Racket says "#%app: missing procedure expression; probably originally (), which is an illegal empty application in: (#%app)". I imagine just about any other compiler would do something similar. – dfeuer May 22 '14 at 05:53
  • You can fix this by adding a character: `(1)` will compile just fine but crash immediately. – dfeuer May 22 '14 at 05:56
  • @dfeuer: That is correct :) IIRC, Chez actually allows this, but it will not crash. It interprets it as `'()`. – leppie May 22 '14 at 06:10
  • Interpreting `()` as `'()` is *bizarre* in Scheme, although I believe it is traditional in LISP. – dfeuer May 22 '14 at 06:30
  • @dfeuer: I guess it was their choice as `()` is meaningless. Just tested again, and it is now marked as invalid syntax (maybe a bug or artefact from older version) in both program and REPL. – leppie May 22 '14 at 06:47
4

Perl in only 2 chars.

&a
Undefined subroutine &main::a called at test.pl line 1.
Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
3

In C, 33 characters:

int main(void){return*((int*)0);}
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • you could omit the `void` in the function declaration... not best but. – jldupont Nov 20 '09 at 13:36
  • Not if you want to follow the standard, you can't. 'int main(int,char*[])' and 'int main(void)' are the only two guaranteed forms. – paxdiablo Nov 20 '09 at 13:38
  • 1
    returning a dereferenced null pointer, even without the assignment, will do the trick. – Mikeage Nov 20 '09 at 13:41
  • @paxdiablo: according to WHAT standard? I'm pretty sure it's legal to omit declare `int x(void) {...}` as `x() {...}` in at least C89, C++ is another story altogether. – falstro Nov 20 '09 at 13:49
  • C99 which is the current standard and C1x draft both state that those two forms are the only guaranteed ones. – paxdiablo Nov 20 '09 at 13:51
  • @paxdiablo: there you go, C89 is also C. It's just not C99. Just because there's a C99 doesn't stop C89 from being C. – falstro Nov 20 '09 at 13:53
  • actually Mikeage is onto some shorter solution. – Don Johe Nov 20 '09 at 13:55
3

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...)

lc.
  • 113,939
  • 20
  • 158
  • 187
  • QBasic doesn't compile, QuickBASIC does :) – Matthew Groves Nov 20 '09 at 13:51
  • Completely illiteral in Q.*Basic I do recognize the connection to the other scripting languages. The QBasic looses points in crashability due to the (completely unnecessary, of course *g*) question mark and space needed to answer a simple question with undefined answer. – Don Johe Nov 20 '09 at 15:51
  • 1
    I don't believe the space is necessary. – recursive Dec 27 '09 at 18:54
3

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
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
3
main = undefined

In Haskell.

Rayne
  • 31,473
  • 17
  • 86
  • 101
2

F# 3 characters

1/0

Does compile, though gives a warning.

David
  • 12,451
  • 1
  • 22
  • 17
  • 1
    We may call it the almost ultimate script-crasher. ;-) – Don Johe Nov 20 '09 at 15:53
  • Ah..yes, but in Haskell 1/0 computes to Infinity. Begone, ye lousy scripters! – user178476 Nov 25 '09 at 20:40
  • 1
    @unknown(yahoo): Haskell would be mathematically wrong. 1/0 is not infinity, it is undefined - rearranging the equation: infinity * 0 == 1? Unlikely. I vaguely remember a lecture in my engineering course where the lecturer constructed a system where a division by zero had a valid result. – Skizz Dec 11 '09 at 13:53
  • `1/0` computes to `Infinity`, provided it is of the correct type. `Float` and `Double` will do, whereas `Integer` will complain (because it's not `Fractional` and doesn't support `/`), as will `Rational` (because zero denominator is not allowed). `Float` and `Double` types exhibit IEEE-defined behaviour, having `NaN` and `Infinity` as possible values, and are totally incorrect mathematically in that `100000000000000000000000000000000000000 + 1.0 == 100000000000000000000000000000000000000` evaluates to `True`. :-) – Michał Marczyk Jan 14 '10 at 05:53
2

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]: ?
gwell
  • 2,695
  • 20
  • 20
2

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'.

elmattic
  • 12,046
  • 5
  • 43
  • 79
2

PostScript, 1 character

Like GolfScript:

*

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

KirarinSnow
  • 1,062
  • 2
  • 9
  • 22
1
int main () { int n = 0; return 1 / n; }
1

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;
};
Naveen
  • 74,600
  • 47
  • 176
  • 233
  • Due to the condition it cannot be accepted as a "general" solution. But pretty straight forward it is. – Don Johe Nov 20 '09 at 15:48
1

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..)

Ether
  • 53,118
  • 13
  • 86
  • 159
1

C#, 37 chars

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

Explodes stack.

leppie
  • 115,091
  • 17
  • 196
  • 297
1

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.

Community
  • 1
  • 1
Jack V.
  • 1,381
  • 6
  • 20
1

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
Skizz
  • 69,698
  • 10
  • 71
  • 108
1

Groovy 3 characters

1/0

Produces

java.lang.ArithmeticException: / by zero

Dónal
  • 185,044
  • 174
  • 569
  • 824
1

NASM

mov al, [0]

$ nasm -f elf64 -o crash.o crash.asm
$ ld -o crash crash.o
$ ./crash
Community
  • 1
  • 1