5

Out of curiousity, are all infinite loops bad?

  • What are the bad effects and consequences that will happen if you run an infinite loop?
  • Also, if they are not all bad, could you please give some examples when they will serve a meaningful purpose?
  • And do they need to have something to close the instance? For example, we always close the StreamReader after using it in Java (not sure why).
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Theone
  • 75
  • 3
  • 7

7 Answers7

6

I'm not sure what you mean by "bad".

Infinite loops are common in many scenarios mostly event handler loops where the program sits in an infinite loop and waits for some external event to occur which is handles and goes back to wait. This is the way GUIs and many servers are programmed.

Update They're sufficiently useful to justify a construct just for infinite loops in some languages.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
  • I have come accross articles that spoke of overloading the computer memory when it is inside an infinite loop. Why do the GUI's and servers not overload the memory? – Theone Jun 29 '12 at 06:27
  • I don't know what these articles meant by overload. An infinite loop with just a single sleep statement inside it will not affect the memory of a machine. – Noufal Ibrahim Jun 29 '12 at 06:30
  • 3
    Memory overloading may happen when there is a memory leak inside of an endless loop. – risingDarkness Jun 29 '12 at 06:32
1
  • If you write your program correctly, there are no side effects that just happen because of the loop.
  • on microcontrollers endless loops are used to never reach the end of the program. At the end of a microcontroller program there's most of the time no such thing as an OS that could take over. Then the state is reached, where no defined behaviour exists and the program may do anything.
risingDarkness
  • 698
  • 12
  • 25
1

No, they're not bad, they're actually useful.

It depends whether you left some part of the code that eats up memory as the infinite loop proceeds. Infinite loops are used at almost everything: video games, networking, machine learning, etc. because infinite loops are usually used for getting instantaneous user input/events.

Shinji
  • 13
  • 6
  • The loops you are describing may have not-so-obvious ending conditions but they are *not* infinite. – Holger Jul 29 '14 at 08:21
  • 1
    by the term "infinite" do you mean programs that requires machines to run indefinitely? – Shinji Jul 29 '14 at 08:31
  • No, I mean that for example, event processing loops will terminate on certain events like closing the last window or “Quit” menu item selected and so even stand-alone devices like a router, etc. have conditions (like “shutdown” command received via ssh) on which the loop ends. True infinite loops are very uncommon. – Holger Jul 29 '14 at 08:46
0

Take the example of a simple server, listening for connections

  • Listen for requests
  • Get Request, Spawn thread for that request
  • Rinse and Repeat

This sort of scenario is pretty common, you see it all the time in event handling.

Negative

  • Program will never terminate, unless you kill it manually (also a positive, depending on the situation)

Do they need to have something to close the instance?

  • No, you don't, as mentioned before you can kill it manually but you could implement an input command line to accept a kill command to end gracefully
Robotnik
  • 3,643
  • 3
  • 31
  • 49
0

You will sometimes hear the phrase killer loop, to refer to a badly behaving loop (infinite or otherwise). Typically reserved for loops that inadvertently consume massive amounts of CPU time, memory, or both. All killer loops are bad.

Froot Loops can also be killer loops depending on your diabetic status.

Mobius strips are good infinite loops.

Tod
  • 8,192
  • 5
  • 52
  • 93
0

The term "infinite loop" is most often used for a loop in a program that wouldn't terminate if there were no external actions that could terminate it. In most cases, using whatever the "interrupt key" is to send an interrupt signal to the operating system will prove that it wasn't an infinite loop after all. And often a power surge or power outage will do the same. And on some platforms with some operating systems other interrupt signals may occur and halt the process.

So pseudo-infinite loops are useful for processes you don't want to terminate except by some "outside" influence.

wwwayne
  • 113
  • 1
  • 7
0

In real-time programs, an infinite loop is normal. Take a look at the Arduino IDE - the only two functions exposed are setup() and loop(). It is assumed that loop() will never exit unless the power is removed.

kiwiron
  • 1,677
  • 11
  • 17
  • In fact the loop function exits... but it is called again and again forever.. i.e. the main function is basically something like: void main() { setup(); while(1) loop(); } – frarugi87 Jul 30 '14 at 09:09