English 中文(简体)
Code-Golf: What is the shortest program that compiles and crashes?
原标题:
  • 时间:2009-11-20 13:29:14
  •  标签:
  • code-golf

Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.

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

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. :P

using python:

1/0

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

$ 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

Perl

die
Died at test line 1.

die

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

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

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"

Brainf*ck

5 characters

+[>+]

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

Factor (or Forth)

.

"Data stack underflow"

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);}}

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.

In C, 20 characters:

void main(){main();}

Update: Suggested by roe, 15 characters:

main(){main();}

Note: Tested with VC++ 2008.

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.

Try this in assembly:

push 0
ret

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

in windows powershell:

throw

Perl in only 2 chars.

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

Scheme:

(1)

Filler text to make this longer.

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
main = undefined

In Haskell.

F# 3 characters

1/0

Does compile, though gives a warning.

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]: ?

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 .

PostScript, 1 character

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;
};

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





相关问题
Code Golf: Ulam Spiral

The Challenge The shortest code by character count to output Ulam s spiral with a spiral size given by user input. Ulam s spiral is one method to map prime numbers. The spiral starts from the number 1 ...

Turing Machine Code Golf

Ok guys, today s goal is to build a Turing machine simulator. For those that don t know what it is, see the Wikipedia article. The state table we are using today is found at the end of the Formal ...

Code Golf: Fractran

The Challenge Write a program that acts as a Fractran interpreter. The shortest interpreter by character count, in any language, is the winner. Your program must take two inputs: The fractran program ...

Running a fastest-algorithm competition

I d like to run competitions like code golf competitions, but the winner would have the fastest algorithm, not the smallest code. One fair way to measure speed of an algorithm is to use a neutral ...

Code Golf: Sierpinski s Triangle

The challenge The shortest code, by character count to output an ASCII representation of Sierpinski s Triangle of N iterations made from the following ASCII triangle: / /__ Input is a single ...

How to convert numbers to words in Erlang?

I found this interesting question about converting numbers into "words": Code Golf: Number to Words I would really like to see how you would implement this efficiently in Erlang.

热门标签