问题
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).
回答1:
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.
回答2:
- 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.
回答3:
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.
回答4:
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
回答5:
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.
回答6:
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.
回答7:
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.
来源:https://stackoverflow.com/questions/11256993/are-all-infinite-loops-bad