{"id":1209,"date":"2022-08-30T15:14:12","date_gmt":"2022-08-30T15:14:12","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/code-golf-what-is-the-shortest-program-that-compiles-and-crashes-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:14:12","modified_gmt":"2022-08-30T15:14:12","slug":"code-golf-what-is-the-shortest-program-that-compiles-and-crashes-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/code-golf-what-is-the-shortest-program-that-compiles-and-crashes-collection-of-common-programming-errors\/","title":{"rendered":"Code-Golf: What is the shortest program that compiles and crashes?-Collection of common programming errors"},"content":{"rendered":"<p>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 &#8220;crash&#8221; we mean that the program stops with an error condition (a core dump for example).<\/p>\n<p>Is there a language that crashes faster (7 chars) than C using a gcc compiler? [I leave this answer for somebody to find.]<\/p>\n<p>(It should be allowable to use compiler flags. Otherwise 7 wouldn&#8217;t work nowadays, compiler checks became much better.)<\/p>\n<p>[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.<\/p>\n<ol>\n<li>\n<p>Bah &#8211; I can crash C in 5 characters:<\/p>\n<pre><code>main;\n<\/code><\/pre>\n<p>This declares an implicit <code>int<\/code> variable called &#8216;main&#8217;. It&#8217;s global so the variable has an initial value of <code>0<\/code>. It&#8217;s C the names aren&#8217;t decorated &#8211; so the linker doesn&#8217;t realize that it&#8217;s a var and not a function.<\/p>\n<p>GCC gave me a warning &#8211; but that&#8217;s all.<\/p>\n<pre><code>$ gcc crash.c \ncrash.c:1: warning: data definition has no type or storage class\n$ .\/a.exe\nSegmentation fault (core dumped)\n$\n<\/code><\/pre>\n<\/li>\n<li>\n<p>Crash with <code>0<\/code> characters:<\/p>\n<pre><code>$ &gt; golf.c\n$ gcc -Wl,--defsym=main=0 golf.c\n$ .\/a.out\nSegmentation fault\n<\/code><\/pre>\n<\/li>\n<li>\n<p>I wonder if this counts&#8230;<\/p>\n<pre><code>a\n<\/code><\/pre>\n<p>This is in JavaScript. This gives the <strong>runtime<\/strong> error of &#8220;object not found&#8221;. Since JavaScript is a dynamic language, syntactically this is actually correct. Still feels like twisting the rules. \ud83d\ude1b<\/p>\n<\/li>\n<li>\n<p>From cmd prompt in windows create file a.com containing byte F4, x86 halt instruction:<\/p>\n<pre><code>F:\\&gt;debug\n-a 100\n0BFD:0100 hlt\n0BFD:0101\n-r cx\nCX 0000\n:1\n-n a.com\n-w\nWriting 00001 bytes\n-q\n\nF:\\&gt;a.com\n<\/code><\/pre>\n<p>The NTVDM CPU has encountered illegal instruction<\/p>\n<\/li>\n<li>\n<pre><code>$ cat &gt; crash.S\nhlt\n$ as -o crash.o crash.S\n$ ld crash.o\nld: warning: cannot find entry symbol _start; defaulting to 0000000008048054\n$ .\/a.out\nSegmentation fault\n<\/code><\/pre>\n<\/li>\n<li>\n<pre><code>die\n<\/code><\/pre>\n<pre>Died at test line 1.<\/pre>\n<h3>die<\/h3>\n<blockquote>\n<p>prints the value of LIST to STDERR and exits with the current value of $! (errno).<\/p>\n<\/blockquote>\n<\/li>\n<li>\n<p>If you&#8217;re at a computer store that has TI-89s, you can crash one by typing this in:<\/p>\n<pre><code>Exec \"00000000\"\n<\/code><\/pre>\n<p>(that&#8217;s 8 zeros)<\/p>\n<p>It will yield &#8220;Illegal Instruction&#8221;. Press 2nd+Left+Right+ON to reset the calc.<\/p>\n<p>If you want to have more fun, do this:<\/p>\n<pre><code>Exec \"4E4A4E750000\"\n<\/code><\/pre>\n<p>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&#8217;s OS draws it back, so to clean up after yourself, reset per the instructions above, or do this:<\/p>\n<pre><code>Exec \"307C56E670FF20C020C020C020C020C04E750000\"\n<\/code><\/pre>\n<\/li>\n<li>\n<p>Commodore 64 BASIC:<\/p>\n<pre><code>poke 2,2:sys2\n<\/code><\/pre>\n<p>or shorter (using PETSCII graphic-char shortcuts):<\/p>\n<pre><code>pO2,2:sY2\n<\/code><\/pre>\n<p>(crash: $02 invalid opcode on MOS\/CSG6510). Actually it can be done in 7 bytes (3-instructions):<\/p>\n<pre><code>lda #$02\nsta $02\njmp $0002\n<\/code><\/pre>\n<\/li>\n<li>\n<pre><code>+[&gt;+]\n<\/code><\/pre>\n<p>It will take it a while, but eventually the program will run out of memory and inevitably crash.<\/p>\n<\/li>\n<li>\n<p>In C, 20 characters:<\/p>\n<pre><code>void main(){main();}\n<\/code><\/pre>\n<p>Update: Suggested by roe, 15 characters:<\/p>\n<pre><code>main(){main();}\n<\/code><\/pre>\n<p><em>Note: Tested with VC++ 2008.<\/em><\/p>\n<\/li>\n<li>\n<p>How about <code>java Z<\/code>? If no file exists it will &#8220;crash&#8221; with a <code>java.lang.NoClassDefFoundError<\/code>. So my answer is zero letters. If that is not valid then&#8230;<\/p>\n<pre><code>class T{}\n<\/code><\/pre>\n<p>Would &#8220;crash&#8221; with $ java T Exception in thread &#8220;main&#8221; java.lang.NoSuchMethodError: main<\/p>\n<p>If you want something that actually runs, then if you are willing to abuse things a little<\/p>\n<pre><code>class T{static {int i =1\/0;}}\n<\/code><\/pre>\n<p>Else<\/p>\n<pre><code>class T{public static void main(String[]a){main(a);}}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>to quote this answer:<\/p>\n<blockquote>\n<p>All these answers and no Befunge? I&#8217;d wager a fair amount it&#8217;s shortest solution of them all:<\/p>\n<pre><code>1\n<\/code><\/pre>\n<p>Not kidding. Try it yourself: http:\/\/www.quirkster.com\/js\/befunge.html<\/p>\n<p>EDIT: I guess I need to explain this one. The 1 operand pushes a 1 onto Befunge&#8217;s internal stack and the lack of anything else puts it in a loop under the rules of the language.<\/p>\n<p>Using the interpreter provided, you will eventually&#8211;and I mean eventually&#8211;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&#8211;as is the case with most of the languages below&#8211;this program would cause a more noticeable overflow faster.<\/p>\n<\/blockquote>\n<\/li>\n<li>\n<p>Try this in assembly:<\/p>\n<pre><code>push 0\nret\n<\/code><\/pre>\n<p>of course add the all other garbage to compile into an application.<\/p>\n<\/li>\n<li>\n<p>in windows powershell:<\/p>\n<pre><code>throw\n<\/code><\/pre>\n<\/li>\n<li>\n<p>Late, but whatever. PHP, 32 characters.<\/p>\n<pre><code>$r=function($z){$z($z);};$r($r);\n<\/code><\/pre>\n<p>gives <code>Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 261900 bytes) in ...<\/code><\/p>\n<p>Technically, I could also do it in 29 with<\/p>\n<pre><code>$r=function($z){$z();};$r(0);\n<\/code><\/pre>\n<p>But that isn&#8217;t as much fun as infinite recursion. That, and I don&#8217;t think &#8220;undefined function&#8221; errors should count as &#8220;compiling&#8221; in a scripting language, otherwise:<\/p>\n<pre><code>Z();\n<\/code><\/pre>\n<p>would be the smallest solution.<\/p>\n<p>Also, instead of crashing, how about (as a script) surpassing max execution time? 8 chars:<\/p>\n<pre><code>for(;;);\n<\/code><\/pre>\n<p>My original take on that had a <code>$i++<\/code> in the third expression of the for loop, but because PHP treats all integers as signed, instead of overflowing, it just goes negative.<\/p>\n<\/li>\n<li>\n<p>In QBasic:<\/p>\n<pre><code>? 1\/0\n<\/code><\/pre>\n<p>(At least I <em>think<\/em> it&#8217;ll still compile and then crash with divide-by-zero; it&#8217;s been quite some time&#8230;)<\/p>\n<\/li>\n<li>\n<p>In C, 33 characters:<\/p>\n<pre><code>int main(void){return*((int*)0);}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>Golfscript &#8211; 1 Char<\/p>\n<p>Lots of operators can do it, eg<\/p>\n<pre><code>*\n<\/code><\/pre>\n<pre>\n(eval):1:in `initialize': undefined method `class_id' for nil:NilClass (NoMethodError)\nfrom ..\/golfscript.rb:285:in `call'\nfrom ..\/golfscript.rb:285:in `go'\nfrom (eval):1:in `initialize'\nfrom ..\/golfscript.rb:285:in `call'\nfrom ..\/golfscript.rb:285:in `go'\nfrom ..\/golfscript.rb:477\n<\/pre>\n<\/li>\n<li>\n<p>1\/0<\/p>\n<p>Does compile, though gives a warning.<\/p>\n<\/li>\n<li>\n<p>The divide by zero does not cause problems in Lua, but here something just as short:<\/p>\n<pre><code>a()\n<\/code><\/pre>\n<h3>gives:<\/h3>\n<pre>\nlua: isort.lua:1: attempt to call global 'a' (a nil value)\nstack traceback:\n    a.lua:1: in main chunk\n    [C]: ?\n<\/pre>\n<\/li>\n<li>\n<pre><code>box 0 :?&gt; unit\n<\/code><\/pre>\n<p>Compiles without a warning. Crashes with: System.InvalidCastException: Unable to cast object of type &#8216;System.Int32&#8217; to type &#8216;Microsoft.FSharp.Core.Unit&#8217;.<\/p>\n<\/li>\n<li>\n<pre><code>main = undefined\n<\/code><\/pre>\n<p>In Haskell.<\/p>\n<\/li>\n<li>\n<p>Like GolfScript:<\/p>\n<pre><code>*\n<\/code><\/pre>\n<p>Syntactically legal, but crashes during runtime because the token <code>*<\/code> is not defined (different reason than why GolfScript crashes).<\/p>\n<\/li>\n<li>\n<pre><code>int main () { int n = 0; return 1 \/ n; }\n<\/code><\/pre>\n<\/li>\n<li>\n<p>It depends upon the allowed max stack size for a thread. But it does crash when compiled using VC9:<\/p>\n<pre><code>int main()\n{\n    int a[10000000];\n    return 0;\n};\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:09:58. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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 &#8220;crash&#8221; 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1209","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1209"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1209\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}