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

This is a little funny. Can you develop the shortest program that compiles, but does nothing except immediately after a crash? Therefore, by "crash" we mean that the program stops with an error condition (for example, a kernel dump).

Is there a language that works faster (7 characters) than C using the gcc compiler? [I leave this answer for someone to find.]

(It must be permissible to use compiler flags, otherwise 7 will not work at present, compiler checks have become much better.)

[evaluation of results] I cannot mark one answer, because for several languages ​​there are several correct ones. It would be unfair to disqualify one answer for another. Please use votes to select the best answers.

+41
programming-languages code-golf
Nov 20 '09 at 13:29
source share
35 answers
  • one
  • 2

Bah - I can split C into 5 characters:

main; 

This declares an implicit int variable called "main". It is global, so the variable has an initial value of 0 . These C names are not decorated - therefore, the linker does not understand that this is var, 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) $ 
+128
Nov 20 '09 at 14:19
source share

Failure with characters 0 :

 $ > golf.c $ gcc -Wl,--defsym=main=0 golf.c $ ./a.out Segmentation fault 
+67
Nov 20 '09 at 16:06
source share

I wonder if this counts ...

 a 

This is in JavaScript. This gives the runtime error "object not found". Because JavaScript is a dynamic language, this is really syntactically correct. It still seems to be distorting the rules .: P

+49
Nov 20 '09 at 13:46
source share

using python:

 1/0 
+47
Nov 20 '09 at 13:40
source share

X86 machine code: 1 byte

From the cmd command line in windows, create the a.com file containing the F4 bytes, instructions for stopping x86:

 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 

NTVDM processor encountered illegal instruction

+33
Nov 20 '09 at 21:22
source share
 $ 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 
+24
Nov 20 '09 at 13:39
source share

Perl

 die 
  Died at test line 1. 



die

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

+23
Nov 20 '09 at 15:56
source share

Commodore 64 BASIC:

 poke 2,2:sys2 

or shorter (using PETSCII graphic <char graphic labels):

 pO2,2:sY2 

(Failure: Invalid opcode $ 02 on MOS / CSG6510). In fact, this can be done in 7 bytes (3 instructions):

 lda #$02 sta $02 jmp $0002 
+11
Nov 22 '09 at 0:33
source share

If you are in a computer store with a TI-89, you can collapse it by entering it in:

 Exec "00000000" 

(which is 8 zeros)

This will give "Inappropriate Instructions." Press 2nd + Left + Right + ON to reset calc.

If you want more fun, do the following:

 Exec "4E4A4E750000" 

This launches a hidden hardware test menu, including memory tests, LCD tests (draws chessboards, etc.) and much more. Unfortunately, the status bar is deleted, and nothing in the OS returns it, so to clear after yourself, reset in accordance with the above instructions, or do the following:

 Exec "307C56E670FF20C020C020C020C020C04E750000" 
+11
Nov 22 '09 at 1:25
source share

Brainf * ck

5 characters

 +[>+] 

This will take some time, but eventually the program will end with memory and inevitably work.

+10
Nov 20 '09 at 18:09
source share

Factor (or Forth)

 .

"Lack of data stack"

+8
Nov 23 '09 at 17:52
source share

What about java Z ? If the file does not exist, it "crashes" using java.lang.NoClassDefFoundError . So my answer is zero letters. If this is not valid, then ...

 class T{} 

There will be a "crash" with $ java T Exception in the thread "main" java.lang.NoSuchMethodError: main

If you want something that really works, then if you want to offend things a little

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

Else

 class T{public static void main(String[]a){main(a);}} 
+7
Nov 20 '09 at 13:40
source share

to quote this answer :

All these answers and the lack of Befunge? I would dial a minimum solution to all of them:

 1 

I'm not kidding. Try it yourself: http://www.quirkster.com/js/befunge.html

EDIT: I think I need to explain this one. 1 operand clicks 1 on the Exclude internal stack and the absence of anything else puts in a loop according to the rules of the language.

Using the provided translator, you end up - and I mean end up - got to the point where the Javascript Array that represents the Befunge Stack gets too big for the browser to redistribute. If you have a simple Befunge interpreter with a smaller and more limited stack - as is the case with most languages ​​below - this program will cause a more noticeable overflow faster.

+7
Nov 21 '09 at 5:55
source share

In C, 20 characters:

 void main(){main();} 

Update: proposed by icon, 15 characters:

 main(){main();} 

Note. Tested with VC ++ 2008.

+6
Nov 20 '09 at 13:34
source share

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 this at 29 using

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

But this is not as fun as endless recursion. This, and I don’t think that the “undefined function” errors should be considered as “compilation” in the scripting language, otherwise:

 Z(); 

will be the smallest solution.

Also, instead of crashing, how about (like a script) exceed the maximum execution time? 8 characters:

 for(;;); 

My initial take, which had $i++ in the third expression of the for loop, but because PHP treats all integers as signed rather than overflowing, is just negative.

+5
Nov 24 '09 at 18:28
source share

Try this in the assembly:

 push 0 ret 

Of course, add all the other garbage to compile into the application.

+4
Nov 20 '09 at 13:34
source share

in windows powershell:

 throw 
+4
Nov 20 '09 at 13:40
source share

Scheme:

 (1) 

Fill in the text to make it last longer.

+4
Nov 20 '09 at 18:32
source share

Perl is just 2 characters.

 &a 
  Undefined subroutine & main :: a called at test.pl line 1. 
+4
Nov 22 '09 at 5:15
source share

In C, 33 characters:

 int main(void){return*((int*)0);} 
+3
Nov 20 '09 at 13:33
source share

In QBasic:

 ? 1/0 

(At least I think that it will still compile, and then break with division by zero, it was quite a long time ago ...)

+3
Nov 20 '09 at 13:38
source share

Golfscript - 1 Char

Many operators can do this, for example

 * 
 (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-00-0077
+3
Nov 24 '09 at 23:32
source share
 main = undefined 

In Haskell.

+3
Nov 24 '09 at 23:35
source share

Symbols F # 3

1/0

Compiles, although it gives a warning.

+2
Nov 20 '09 at 15:33
source share

Lua, 3 characters

Division by zero does not cause problems in Lua, but here is something 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]:?
+2
Nov 20 '09 at 21:51
source share

F #, 11 characters

 box 0 :?> unit 

Compiles without warning. Failure: System.InvalidCastException: Unable to cast an object of type "System.Int32" to enter "Microsoft.FSharp.Core.Unit".

+2
Nov 22 '09 at 1:39
source share

PostScript, 1 character

Like GolfScript:

 * 

It is syntactically legal, but it crashes at runtime because the token * not defined (for another reason, for some reason, with a GolfScript error).

+2
Dec 15 '09 at 5:30
source share
 int main () { int n = 0; return 1 / n; } 
+1
Nov 20 '09 at 13:34
source share

It depends on the allowed maximum stack size for the stream. But it crashes when compiling using VC9:

 int main() { int a[10000000]; return 0; }; 
+1
Nov 20 '09 at 13:35
source share

Perl

3 characters

 1/0 

It produces:

 Illegal division by zero at crash.pl line 1. 

(still looking for something to do it in half ..)

+1
Nov 20 '09 at 18:28
source share
  • one
  • 2



All Articles