Code-Golf: What is the shortest program that compiles and crashes?-Collection of common programming errors
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.
-
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 of0
. 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) $
-
Crash with
0
characters:$ > golf.c $ gcc -Wl,--defsym=main=0 golf.c $ ./a.out Segmentation fault
-
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. 😛
-
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
-
$ 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
-
die
Died at test line 1.
die
prints the value of LIST to STDERR and exits with the current value of $! (errno).
-
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"
-
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
-
+[>+]
It will take it a while, but eventually the program will run out of memory and inevitably crash.
-
In C, 20 characters:
void main(){main();}
Update: Suggested by roe, 15 characters:
main(){main();}
Note: Tested with VC++ 2008.
-
How about
java Z
? If no file exists it will “crash” with ajava.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);}}
-
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.
-
Try this in assembly:
push 0 ret
of course add the all other garbage to compile into an application.
-
in windows powershell:
throw
-
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. -
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…)
-
In C, 33 characters:
int main(void){return*((int*)0);}
-
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
-
1/0
Does compile, though gives a warning.
-
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]: ?
-
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’.
-
main = undefined
In Haskell.
-
Like GolfScript:
*
Syntactically legal, but crashes during runtime because the token
*
is not defined (different reason than why GolfScript crashes). -
int main () { int n = 0; return 1 / n; }
-
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; };
Originally posted 2013-11-10 00:09:58.