Your task is to create the shortest infinite loop!
The point of this challenge is to create an infinite loop producing no output, unlike its possible duplicate. The reason to this is because the code might be shorter if no output is given.
Rules
- Each submission must be a full program.
- You must create the shortest infinite loop.
- Even if your program runs out of memory eventually, it is still accepted as long as it is running the whole time from the start to when it runs out of memory. Also when it runs out of memory, it should still not print anything to STDERR.
- The program must take no input (however, reading from a file is allowed), and should not print anything to STDOUT. Output to a file is also forbidden.
- The program must not write anything to STDERR.
- Feel free to use a language (or language version) even if it's newer than this challenge. -Note that there must be an interpreter so the submission can be tested. It is allowed (and even encouraged) to write this interpreter yourself for a previously unimplemented language. :D
- Submissions are scored in bytes, in an appropriate (pre-existing) encoding, usually (but not necessarily) UTF-8. Some languages, like Folders, are a bit tricky to score - if in doubt, please ask on Meta.
- This is not about finding the language with the shortest infinite loop program. This is about finding the shortest infinite loop program in every language. Therefore, I will not accept an answer.
- If your language of choice is a trivial variant of another (potentially more popular) language which already has an answer (think BASIC or SQL dialects, Unix shells or trivial Brainf**k-derivatives like Alphuck), consider adding a note to the existing answer that the same or a very similar solution is also the shortest in the other language.
- There should be a website such as Wikipedia, Esolangs, or GitHub for the language. For example, if the language is CJam, then one could link to the site in the header like
#[CJam](http://sourceforge.net/p/cjam/wiki/Home/), X bytes. - Standard loopholes are not allowed.
(I have taken some of these rules from Martin Büttner's "Hello World" challenge)
Please feel free to post in the comments to tell me how this challenge could be improved.
Catalogue
This is a Stack Snippet which generates both an alphabetical catalogue of the used languages, and an overall leaderboard. To make sure your answer shows up, please start it with this Markdown header:
# Language name, X bytes
Obviously replacing Language name and X bytes with the proper items. If you want to link to the languages' website, use this template, as posted above:
#[Language name](http://link.to/the/language), X bytes
Now, finally, here's the snippet: (Try pressing "Full page" for a better view.)
var QUESTION_ID=59347;var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";var COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk";var OVERRIDE_USER=41805;var answers=[],answers_hash,answer_ids,answer_page=1,more_answers=true,comment_page;function answersUrl(index){return"//api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+index+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(index,answers){return"//api.stackexchange.com/2.2/answers/"+answers.join(';')+"/comments?page="+index+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:true,success:function(data){answers.push.apply(answers,data.items);answers_hash=[];answer_ids=[];data.items.forEach(function(a){a.comments=[];var id=+a.share_link.match(/\d+/);answer_ids.push(id);answers_hash[id]=a});if(!data.has_more)more_answers=false;comment_page=1;getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:true,success:function(data){data.items.forEach(function(c){if(c.owner.user_id===OVERRIDE_USER)answers_hash[c.post_id].comments.push(c)});if(data.has_more)getComments();else if(more_answers)getAnswers();else process()}})}getAnswers();var SCORE_REG=/<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;var OVERRIDE_REG=/^Override\s*header:\s*/i;function getAuthorName(a){return a.owner.display_name}function process(){var valid=[];answers.forEach(function(a){var body=a.body;a.comments.forEach(function(c){if(OVERRIDE_REG.test(c.body))body='<h1>'+c.body.replace(OVERRIDE_REG,'')+'</h1>'});var match=body.match(SCORE_REG);if(match)valid.push({user:getAuthorName(a),size:+match[2],language:match[1],link:a.share_link,});else console.log(body)});valid.sort(function(a,b){var aB=a.size,bB=b.size;return aB-bB});var languages={};var place=1;var lastSize=null;var lastPlace=1;valid.forEach(function(a){if(a.size!=lastSize)lastPlace=place;lastSize=a.size;++place;var answer=jQuery("#answer-template").html();answer=answer.replace("{{PLACE}}",lastPlace+".").replace("{{NAME}}",a.user).replace("{{LANGUAGE}}",a.language).replace("{{SIZE}}",a.size).replace("{{LINK}}",a.link);answer=jQuery(answer);jQuery("#answers").append(answer);var lang=a.language;lang=jQuery('<a>'+lang+'</a>').text();languages[lang]=languages[lang]||{lang:a.language,lang_raw:lang,user:a.user,size:a.size,link:a.link}});var langs=[];for(var lang in languages)if(languages.hasOwnProperty(lang))langs.push(languages[lang]);langs.sort(function(a,b){if(a.lang_raw.toLowerCase()>b.lang_raw.toLowerCase())return 1;if(a.lang_raw.toLowerCase()<b.lang_raw.toLowerCase())return-1;return 0});for(var i=0;i<langs.length;++i){var language=jQuery("#language-template").html();var lang=langs[i];language=language.replace("{{LANGUAGE}}",lang.lang).replace("{{NAME}}",lang.user).replace("{{SIZE}}",lang.size).replace("{{LINK}}",lang.link);language=jQuery(language);jQuery("#languages").append(language)}}
body{text-align:left!important}#answer-list{padding:10px;width:500px;float:left}#language-list{padding:10px;padding-right:40px;width:500px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="language-list"> <h2>Shortest Solution by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr> </thead> <tbody id="languages"> </tbody> </table> </div> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr> </thead> <tbody id="answers"> </tbody> </table> </div> <table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table>
588 Answers 588
Jelly, 1 byte
ß
Recursively calls the main link. Thanks to tail call optimization, this results in an actual infinite loop.
-
1\$\begingroup\$
01¿, without recursion. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2016年09月10日 10:47:07 +00:00Commented Sep 10, 2016 at 10:47
JavaScript, 8 bytes
for(;;);
Since the expression to evaluate is omitted, it will always evaluate to true (i.e. your classic while(true){} loop in "disguise").
-
\$\begingroup\$ You don't need the ; at then end. \$\endgroup\$Grant Davis– Grant Davis2015年10月09日 14:50:58 +00:00Commented Oct 9, 2015 at 14:50
-
3\$\begingroup\$ @GrantDavis Yes you do. The for loop still expects a statement, and the semicolon tells that there is no statement. \$\endgroup\$Conor O'Brien– Conor O'Brien2016年04月16日 23:26:41 +00:00Commented Apr 16, 2016 at 23:26
-
\$\begingroup\$ Although much longer, my personal favorite is
eval(s="eval(s)")., because it consumes a LOT of memory \$\endgroup\$2020年10月08日 16:43:45 +00:00Commented Oct 8, 2020 at 16:43
Binary Lambda Calculus, 3 bytes
Before I dive into the explanation, let's start with the program itself.
F†€
Trust me, it's pure luck that they're all printable, and I'll get to why it is 2 1/4 bytes instead of 3 after the explanation. I'll explain this by walking through the process I took to create this program.
To start, BLC programs are just lambda calculus programs encoded in a special way. With this in mind, let's begin with the lambda calculus program that enters an infinite loop, known as omega.
(λx.xx)(λx.xx)
This results in an infinite loop because, according to Wikipedia, it reduces to itself after a single beta-reduction. To convert this into BLC, we must first convert it to De Bruijn indices. It converts into the following:
.λ.11λ.11 (The dots after the λs are necessary for BLC but not part of De Bruijn indices)
Okay, now that it's in De Bruijin indices we can now convert it into BLC where λ translates to 00, function application or . translates to 01, and numbers are represented as 1^n0 where n is the number. Knowing this, it translates into the following binary:
01 00 01 10 10 00 01 10 10
This is why it's 2 1/4 bytes. As BLC instructions aren't full bytes (with the exception of 7), it is rare for programs to fit exactly into a certain byte count. To turn this into hex, we have to pad it in order to make it fit into 3 bytes. Doing this yields the following:
46 86 80
There we have the hex dump of our program! It runs in an infinite loop, doesn't run out of memory to my knowledge, doesn't output anything, and is a complete program that can be saved and run by piping the contents of the file to the official interpreter. You can also pipe the binary text to the interpreter and add the -b flag, to demonstrate that the non-padded version can be run.
Cubix, 0 bytes
Alternatively, any single character does exactly the same.
Cubix is a stack-based 2D language, created by me in March 2016. Cubix differs from ordinary 2D languages in that it's not strictly 2D: the code is wrapped around a cube. Any one-byte program wraps to this cube net:
?
>. . . .
.
where ? represents the character, and the IP (instruction pointer) starts at the arrow. . is simply a no-op, and when the IP reaches the right side of the cube net, it simply wraps back around to the left. Thus, any 1-byte program does nothing forever, no matter what the character is (as long as it's not whitespace).
Thanks to @ErikGolferエリックゴルファー for reminding me that the empty program does the same. When Cubix was originally created, this didn't work, because the interpreter created a cube of size 0 and threw an error when it tried to run. This was fixed a while ago, and it is now impossible to cause an error in Cubix.
Tcl/Tk, 0 bytes
Execute any empty file with the wish interpreter instead of tclsh.
Java6 : 25 Bytes
In Java 6 and previous versions you can execute static initialization block without having main in your class file.
class A{static{for(;;);}}
-
3\$\begingroup\$ Wouldn't
for(;;);work just as well asfor(int i=1;i>0;);, or is there some restriction in Java 6? \$\endgroup\$ETHproductions– ETHproductions2015年10月04日 00:00:23 +00:00Commented Oct 4, 2015 at 0:00 -
2\$\begingroup\$
while(true)is shorter than and as valid asfor(int i=1;i>0;). \$\endgroup\$user8397947– user83979472016年05月29日 15:47:15 +00:00Commented May 29, 2016 at 15:47 -
1\$\begingroup\$ Why is
System.exit(0);needed? \$\endgroup\$Oskar Skog– Oskar Skog2021年08月14日 09:07:06 +00:00Commented Aug 14, 2021 at 9:07 -
\$\begingroup\$ @OskarSkog It's not needed, just added to terminate the loop. Let me correct. \$\endgroup\$Akash Thakare– Akash Thakare2021年08月14日 11:06:38 +00:00Commented Aug 14, 2021 at 11:06
Processing, 8 bytes
for(;;);
This is based on Geobit's answer in Java
Although the code below is not the shortest, it is one of Processing's specialties.
void draw(){}
This draw statement repeats itself over and over again. It is one of the differences between Processing and Java.
-
5\$\begingroup\$ You can also add JavaScript to your title, code stays the same=) \$\endgroup\$flawr– flawr2015年10月02日 18:11:50 +00:00Commented Oct 2, 2015 at 18:11
CJam, 4 bytes
1{}h
Put a 1 on the stack, and loop until 1 is no longer truthy. Using h means that the number is never popped.
-
5\$\begingroup\$ Alternatively,
{1}g. \$\endgroup\$Martin Ender– Martin Ender2015年10月02日 19:01:49 +00:00Commented Oct 2, 2015 at 19:01
-
\$\begingroup\$ I just came over here to post this, but then I noticed it in the catalogue snippet. +1 \$\endgroup\$ETHproductions– ETHproductions2015年10月02日 22:41:44 +00:00Commented Oct 2, 2015 at 22:41
Rust, 17 bytes
Didn't see one in rust so:
fn main(){loop{}}
real original and interesting and so different from all the other entries.
-
1\$\begingroup\$ When I try this on the playground, it prints to stderr. \$\endgroup\$MutantOctopus– MutantOctopus2018年01月23日 13:10:13 +00:00Commented Jan 23, 2018 at 13:10
-
\$\begingroup\$ @BHustus What is it printing to stderr? I'm not getting anything. \$\endgroup\$Liam– Liam2018年01月23日 22:23:04 +00:00Commented Jan 23, 2018 at 22:23
-
\$\begingroup\$ Besides the standard compilation details, running results in
/root/entrypoint.sh: line 7: 6 Killed timeout --signal=KILL ${timeout} "$@"via this link: play.rust-lang.org/… That -might- just be the playground and not the actual compiled product though, I didn't have the chance to test a compiled version. \$\endgroup\$MutantOctopus– MutantOctopus2018年01月24日 00:05:31 +00:00Commented Jan 24, 2018 at 0:05 -
\$\begingroup\$ @BHustus It's an infinite loop and so the playground kills it after a timeout because they don't want to waste time running my code endlessly. If you run this on a desktop that shouldn't happen. \$\endgroup\$Liam– Liam2018年01月24日 02:24:43 +00:00Commented Jan 24, 2018 at 2:24
-
\$\begingroup\$ In that case, disregard what I said. \$\endgroup\$MutantOctopus– MutantOctopus2018年01月24日 05:18:15 +00:00Commented Jan 24, 2018 at 5:18
-
\$\begingroup\$ I just took the interpreter for a spin, and somehow I get the feeling the looping is not in the actual code execution, but in the interpreter checking for infinite loops. Can't confirm until I manage to understand this code properly though (I don't think that affects the validity of the answer, but it's interesting...) \$\endgroup\$Sp3000– Sp30002015年11月04日 13:00:51 +00:00Commented Nov 4, 2015 at 13:00
-
1\$\begingroup\$ @Sp3000 Took it for a spin. Nice pun! \$\endgroup\$user63571– user635712017年01月30日 17:29:10 +00:00Commented Jan 30, 2017 at 17:29
Linux shebang, 5 bytes
#!./a
Must be named as a.
Will run out of memory, but this is an infinite loop.
-
2\$\begingroup\$ I can't believe nobody's thought of this! +1 \$\endgroup\$Riker– Riker2016年06月08日 09:02:59 +00:00Commented Jun 8, 2016 at 9:02
-
\$\begingroup\$ Which system? On Ubuntu
execis a Bash built-in, so I can't specify it as script interpreter. \$\endgroup\$manatwork– manatwork2016年06月08日 09:09:18 +00:00Commented Jun 8, 2016 at 9:09 -
\$\begingroup\$ @manatwork oh, sorry, i forgot that it is built-in. fixed \$\endgroup\$usernameak– usernameak2016年06月08日 09:11:34 +00:00Commented Jun 8, 2016 at 9:11
-
2\$\begingroup\$ Not runs out of memory: "bash: ./a: ./: bad interpreter: Too many levels of symbolic links". (Though no actual symlink involved.) Well, infinite for 0.001 second. \$\endgroup\$manatwork– manatwork2016年06月08日 12:55:34 +00:00Commented Jun 8, 2016 at 12:55
-
1\$\begingroup\$ Not working ...bad interpreter... better put
./aalone in the script. If you want to avoid out of memory use./a&. That's fun if youpsafter. That last script will put your computer in a race of creating processes and managing dead processes \$\endgroup\$Emmanuel– Emmanuel2016年09月27日 22:23:06 +00:00Commented Sep 27, 2016 at 22:23
Commodore Basic, 3 bytes
1R╭
PETSCII substitution: ╭ = SHIFT+U, ungolfs to 1 RUN.
Taking advantage of Commodore Basic's shortcut forms and the fact that any immediate-mode command can also be used in a program, this code simply runs itself, forever.
Alternatively, a more thoroughly infinite loop is the immediate-mode command
S|7
(PETSCII: | = SHIFT+Y, ungolfs to SYS 7).
This transfers execution to memory location 0x0007. The BASIC interpreter stores the current search character here; when running the above code, this character is the double-quotation mark with byte value 0x22. Opcode 0x22 is an undocumented HALT opcode, which works by putting the 6510's micro-operation interpreter into an infinite loop. The only way out of this loop is to reset the computer.
-
\$\begingroup\$ How about a note on what
RandShift+UrespectivelySandShift+Ywill expand to? And You could submit a 6510 assembler program. \$\endgroup\$Titus– Titus2016年12月01日 00:36:59 +00:00Commented Dec 1, 2016 at 0:36 -
\$\begingroup\$ On the Commodore 64 and VIC-20 you can also use
0 GOTO- if you do not specify a number thenGOTOassumes that you meanGOTO 0. As Commodore BASIC on the C64 and VIC are recycled from earlier CBM/PET machines, this may also work on those computers. It does not work on the C128 in native mode though. \$\endgroup\$Shaun Bebbers– Shaun Bebbers2017年01月26日 11:36:56 +00:00Commented Jan 26, 2017 at 11:36
TI-BASIC, 2 bytes
If recursion is allowed (is it a loop?)
prgmA
Runs a program named 'A', hence the program must be named the same. Some research revealed that prgm is 1 byte, plus 1 byte for A
Runs out of memory pretty quickly, but that doesn't seem to be addressed above.
-
1\$\begingroup\$
:is the mandatory beginning of each line, and I believe that isn't counted. Also, the title shouldn't be counted. Do you count a .js file name into the byte count? I'll look into theprgmtoken, though it's a single command. \$\endgroup\$Stretch Maniac– Stretch Maniac2015年10月03日 00:27:56 +00:00Commented Oct 3, 2015 at 0:27 -
\$\begingroup\$ In this case, however, the actual title is irrelevant as long as it matches the command. \$\endgroup\$Stretch Maniac– Stretch Maniac2015年10月03日 00:39:09 +00:00Commented Oct 3, 2015 at 0:39
-
1\$\begingroup\$ Isn't it three bytes: 1 for the
Ain the program name \$\endgroup\$The Fifth Marshal– The Fifth Marshal2017年07月31日 21:44:59 +00:00Commented Jul 31, 2017 at 21:44 -
4\$\begingroup\$ Why do people always add
:to the start of each line? It's just a part of the editor, not the actual code. \$\endgroup\$12Me21– 12Me212018年02月12日 15:23:50 +00:00Commented Feb 12, 2018 at 15:23 -
1\$\begingroup\$ @raddish0 "Even if your program runs out of memory eventually, it is still accepted as long as it is running the whole time from the start to when it runs out of memory" \$\endgroup\$The Fifth Marshal– The Fifth Marshal2020年06月28日 21:26:10 +00:00Commented Jun 28, 2020 at 21:26
x86-16 Assembly, IBM PC DOS, 2 bytes
There's quite a few machine code answers, but thought I'd contribute a few different takes.
56 PUSH SI ; push 100H onto stack
C3 RET ; pop stack and jump to address (PUSH SI)
On DOS when a program is started the SI register contains the starting address of the program (generally 100H). Push that onto the stack and execute a RET which will pop the stack and jump to that address. Big ol' loop.
x86-16 Assembly, 1 byte
F4 HLT ; halt the processor and wait for signal
Okay, so this may depend a little on your definition of infinite loop. The HLT instruction (specifically in 8086 real mode) puts the processor in HALT state and awaits a signal in the form of BINIT#, INIT#, RESET# or interrupt (ref). While it's not technically executing our code, it is in a microcode loop of sorts waiting for one of those signals.
Motorola 6800, 2 bytes
9D DD HCF ; halt and catch fire
From Wikipedia:
When this instruction is run the only way to see what it is doing is with an oscilloscope. From the user's point of view the machine halts and defies most attempts to get it restarted. Those persons with indicator lamps on the address bus will see that the processor begins to read all of the memory, sequentially, very quickly. In effect, the address bus turns into a 16 bit counter. However, the processor takes no notice of what it is reading... it just reads.
C (GNU-EFI), 0 bytes
Yes, a C GNU-EFI program won't exit. You need to return for it to exit. Will not say anything, just hang.
And yes, it works with a standart GNU-EFI Makefile.
I used this one:
ARCH = $(shell uname -m | sed s,i[3456789]86,ia32,)
OBJS = main.o
TARGET = main.efi
EFIINC = /usr/include/efi
EFIINCS = -I$(EFIINC) -I$(EFIINC)/$(ARCH) -I$(EFIINC)/protocol
LIB = /usr/lib
EFILIB = /usr/lib/
EFI_CRT_OBJS = $(EFILIB)/crt0-efi-$(ARCH).o
EFI_LDS = $(EFILIB)/elf_$(ARCH)_efi.lds
CFLAGS = $(EFIINCS) -fno-stack-protector -fpic \
-fshort-wchar -mno-red-zone -Wall
ifeq ($(ARCH),x86_64)
CFLAGS += -DEFI_FUNCTION_WRAPPER
endif
LDFLAGS = -nostdlib -znocombreloc -T $(EFI_LDS) -shared \
-Bsymbolic -L $(EFILIB) -L $(LIB) $(EFI_CRT_OBJS)
all: $(TARGET)
main.so: $(OBJS)
ld $(LDFLAGS) $(OBJS) -o $@ -lefi -lgnuefi
%.efi: %.so
objcopy -j .text -j .sdata -j .data -j .dynamic \
-j .dynsym -j .rel -j .rela -j .reloc \
--target=efi-app-$(ARCH) $^ $@
Hope this counts. And yes I tested this on a virtual machine.
-
1\$\begingroup\$ Welcome to Code Golf! This is interesting, although I'm not sure if it counts as a loop. \$\endgroup\$2021年02月18日 17:33:24 +00:00Commented Feb 18, 2021 at 17:33
-
\$\begingroup\$ But does it loop? \$\endgroup\$Mark– Mark2021年04月24日 23:30:16 +00:00Commented Apr 24, 2021 at 23:30
Aussie++, 47 bytes
G'DAY MATE!
THE HARD YAKKA FOR f IS()<f();>f();
Tested in commit 0a5de7e. The official syntax for this is:
G'DAY MATE!
I RECKON I'LL HAVE A WALKABOUT UNTIL (YEAH, NAH) <
>
CHEERS C***!
However, that UNTIL loop is so verbose it's shorter to use a recursive function.
RISC-V machine code, 2 bytes
For CPUs implementing RV32I/E-C or RV64I-C:
01 a0 — c.j .
For RV32I/E-C CPUs only:
01 20 — c.jal .
RISC-V machine code (no extensions), 4 bytes
There are 4 sets of 32[1] instructions that work for this
6f 00 00 00 - j .
ef 00 00 00 - jal .
6f 01 00 00 - jal x2, .
...
ef 0f 00 00 - jal x31, .
The other 96 options are based on the conditional jump instructions beq, bge and bgeu with the same register as operands
[1]: 16 on RV32E
-
1\$\begingroup\$ Welcome to Code Golf, and nice first answer! \$\endgroup\$Aaroneous Miller– Aaroneous Miller2021年10月29日 19:19:26 +00:00Commented Oct 29, 2021 at 19:19
MUMPS, 1 byte
f
This is the equivalent of for ( ; ; ) ; in C-like languages. It runs from the prompt as is, though, and does not need to be wrapped in a function declaration or any such thing.
GNU dc, 6 bytes
[dx]dx
Tail recursion FTW, which GNU dc supports. Others might not.
-
\$\begingroup\$ That's my length 6 dc snippet :) But on my machine it just crashes. Is that normal? \$\endgroup\$daniero– daniero2015年10月02日 18:27:26 +00:00Commented Oct 2, 2015 at 18:27
-
\$\begingroup\$ Which machine do you have? (and which dc?). I'm running GNU dc on Linux, which supports infinite tail recursion. I think possibly other
dcs don't support any kind of tail recursion, and end up crashing with a stack overflow. \$\endgroup\$Digital Trauma– Digital Trauma2015年10月02日 20:05:57 +00:00Commented Oct 2, 2015 at 20:05 -
\$\begingroup\$ A mac, using the pre-installed dc. It says
dc (GNU bc 1.06) 1.3. I see that it works on a Linux box with the slightly newer version (1.3.95) \$\endgroup\$daniero– daniero2015年10月02日 20:14:29 +00:00Commented Oct 2, 2015 at 20:14
Javascript (8 bytes)
for(;;);
Edit courtody of @KritixiLithos
Javascript ((削除) 10 bytes (削除ここまで))
while(1){}
-
1\$\begingroup\$ Try
for(;;);It has 8 bytes. \$\endgroup\$user41805– user418052015年10月03日 09:29:52 +00:00Commented Oct 3, 2015 at 9:29 -
\$\begingroup\$ @KritixiLithos I forgot that javascript for loops are basically infinite if used wrong. I'm way too used to languages like python which eventually stop for loops. Very clever :) \$\endgroup\$Daffy– Daffy2015年10月04日 10:26:57 +00:00Commented Oct 4, 2015 at 10:26
-
\$\begingroup\$ Although, one can make an infinite for loop in python. \$\endgroup\$The Fifth Marshal– The Fifth Marshal2017年05月12日 23:11:23 +00:00Commented May 12, 2017 at 23:11
Scheme, 12 bytes
A tail-recursive infinite loop seems most appropriate for scheme :-)
(let l()(l))
even though (do(())()) (the CL variant of which is due to @JoshuaTaylor) would be 2 bytes shorter.
z80 Machine Code, 1 byte
c7 ; RST 00h
Or if assuming the code starts at 0000h is cheating, two bytes:
18 fe ; JR -2
These solutions make no assumptions about the rest of the environment's RAM. If it's filled with zero bytes, we are just spinning through NOPs forever, so we could have a 0-byte solution. (Thanks to Thomas Kwa for pointing this out.)
-
\$\begingroup\$ Good point; I've written a note about it. \$\endgroup\$lynn– lynn2015年10月04日 19:20:50 +00:00Commented Oct 4, 2015 at 19:20
-
1\$\begingroup\$ For special fun, fill RAM with 0xDD (or 0xFD). Or maybe this isn't an infinite loop, because it's just one infinitely long opcode. \$\endgroup\$Philip Kendall– Philip Kendall2015年10月06日 16:36:54 +00:00Commented Oct 6, 2015 at 16:36
-
\$\begingroup\$ 1Byte = 76 ; Halt codegolf.stackexchange.com/a/166100/80745 \$\endgroup\$mazzy– mazzy2018年06月04日 10:01:34 +00:00Commented Jun 4, 2018 at 10:01
Fortran, 11 bytes
It is not very creative, but here is the shortest I found:
1goto 1
end
I am pretty sure this is not standards compliant, but it compiles with ifort.
R, 8 bytes
repeat{}
I believe this is the shortest way to make a loop that won't stop. We would need an infinite length list to use a for loop and while(T) is longer than repeat.
-
\$\begingroup\$ I thought I had, but it sure as hell doesn't work -- dunno what I did. Sorry about that. \$\endgroup\$user5957401– user59574012017年10月13日 18:54:49 +00:00Commented Oct 13, 2017 at 18:54
-
2\$\begingroup\$ +1 : This or equivalently
repeat 1(orrepeat xwith x being any valid single character expression) \$\endgroup\$digEmAll– digEmAll2018年06月04日 08:11:16 +00:00Commented Jun 4, 2018 at 8:11 -
\$\begingroup\$ @digEmAll doesn’t
repeat 1output something? \$\endgroup\$JayCe– JayCe2018年06月25日 01:43:11 +00:00Commented Jun 25, 2018 at 1:43 -
\$\begingroup\$ @JayCe: I also thought so, but actually it doesn't... \$\endgroup\$digEmAll– digEmAll2018年06月25日 06:25:08 +00:00Commented Jun 25, 2018 at 6:25
NASM/YASM x86 assembly, 4 bytes
ja $
$ is the address of the current instruction, and ja jumps there if the carry and zero flags are both unset. (i.e. the Above condition is true.) This is the case in x86-64 Linux at process startup.
ja$ just defines a symbol, instead of being an instruction, so the space is not optional. I did test that this works without a trailing newline, so it really is 4 bytes.
Assemble/link with
$ yasm -felf64 foo.asm && ld -o foo foo.o
ld: warning: cannot find entry symbol _start; defaulting to 0000000000400080
The x86-64 ABI used by Linux doesn't guarantee anything about the state of registers at process startup, but Linux's actual implementation zeroes all the registers other than RSP for a newly-execed process. EFLAGS=0x202 [ IF ], so ja (jump if Above) does jump, because the carry and zero flags aren't set. jg (ZF=0 and SF=0) would also work. Other OSes that initialize flags differently might be able to use one of the other one-letter conditions that require a flag bit to be set: jz, jl, jc, jp, js.
Using ja instead of jmp (unconditional jump) makes the source one byte shorter, but the binary is the same size (2 bytes: one opcode byte, one rel8 displacement, for a total size of 344 bytes for a stripped ELF64 binary. See casey's answer for a 45 byte ELF executable if you're interested in small binary size rather than small source size.)
AT&T (PDP-11) Syntax Assembly: 4 bytes
br .
PDP-11 UNIX A.OUT binary output: (削除) 24 (削除ここまで) 18 bytes
0000000 000407 000002 000000 000000 000000 000000 000000 000000
0000020 000777 000000 000000 000004
0000030
This is the output produced by the assembler. As the sizes in the header show, the last three words are not necessary, it can be cut down to the first 18 bytes.
Some modern assemblers do not support the br instruction, so it would be five bytes for jmp .. And executable headers are generally much bigger these days.
Linux x86-64 binary output, after strip: 336 bytes
Now, OSX's assembler is much more strict. You must have a symbol (by default start, but here I use f) for the entry point, which balloons the size of the source. It also requires a newline at the end of the file.
Mac OS X x86-64 Assembly: 17 bytes
.globl f
f:jmp f
Mac OS Mach-O Binary Output: 4200 bytes
-
3\$\begingroup\$ You can get smaller binary output if you golf the ELF header in Linux: see codegolf.stackexchange.com/a/59479/19694 \$\endgroup\$casey– casey2015年10月03日 03:35:02 +00:00Commented Oct 3, 2015 at 3:35
-
\$\begingroup\$ What machine is
br .for? x86? Which assembler? GNU as rejects it for x86-64. In any case, I posted a 4-byte x86 YASM source, which assembles and links into a complete working infinite loop program on Linux:ja $. GNU ld defaults to 0x0400080 if it can't find a_startsymbol, so this really is a complete program. \$\endgroup\$Peter Cordes– Peter Cordes2015年10月06日 21:07:51 +00:00Commented Oct 6, 2015 at 21:07 -
\$\begingroup\$ @PeterCordes PDP-11 - and I assume also VAX, though I haven't had a chance to test it. It's common for older assemblers to use separate mnemonics for short jump instructions. The PDP-11 instruction contains the offset within the instruction word, with a range of even values from -64 to +62. \$\endgroup\$Random832– Random8322015年10月06日 21:08:23 +00:00Commented Oct 6, 2015 at 21:08
-
\$\begingroup\$ @Random832: could you update your answer, then? "AT&T syntax" doesn't specify a machine. For example, there is an i386/amd64 flavour of "AT&T syntax", used by the GNU tools by default.
add 16,ドル %rsp, for example. \$\endgroup\$Peter Cordes– Peter Cordes2015年10月06日 21:10:08 +00:00Commented Oct 6, 2015 at 21:10 -
\$\begingroup\$ My answer goes into detail on the source required to make a usable x86-64 program on GNU/Linux and Mac OSX. By "AT&T" I meant the actual original assembler produced by AT&T, whose syntax is imitated by these other assemblers. \$\endgroup\$Random832– Random8322015年10月06日 21:11:02 +00:00Commented Oct 6, 2015 at 21:11
"", an empty string? \$\endgroup\$