Is a program required to terminate? In other words is a program that that runs forever technically Undefined Behavior? Note this is not about empty loops. Talking about programs that do "stuff" (i.e. observable behavior) forever.
E.g. something like this:
int main()
{
while (true)
{
try
{
get_input(); // calls IO
process();
put_output(); // calls IO, has observable behavior
// never break, exit, terminate, etc
} catch(...)
{
// ignore all exceptions
// don't (re)throw
// never go out of loop
}
}
}
This is more of an academic question, as empirically all sane compilers will generate expected code for the above kind of program (assuming of course no other source of UB). And yes, of course there are a lot of programs that never terminate (os, embeded, servers). However the standard is quirky sometimes, thus the question.
Tangential: many (some?) definitions of "algorithm" require that an algorithm must terminate, i.e. a series of operations that never terminates is not considered an algorithm.
Tangential. The halting problem states that there cannot exist an algorithm to determine if an arbitrary program finishes for an input. However for this particular program since there is no branch that leads to getting out of main, the compiler can easily determine the program will never end. This is however irrelevant as the question is language-lawyer.
Software Engineering C Some of the common ways to terminate a program in C are: exit. _Exit() quick_exit.
So the C family has three ways to end the program: exit(), return, and final closing brace.
The exit function can be used to terminate program execution and return a specified value as program status to the calling program, usually the operating system.
There is nothing in the C++ standard that requires the program, or any given thread, to terminate. The closest thing to that is [intro.progress]p1, which says
The implementation may assume that any thread will eventually do one of the following:
- terminate,
- make a call to a library I/O function,
- perform an access through a volatile glvalue, or
- perform a synchronization operation or an atomic operation.
[ Note: This is intended to allow compiler transformations such as removal of empty loops, even when termination cannot be proven. — end note ]
As long as there is some observable behavior, eventually, or as long as it spends all its time blocked on an I/O operation or another blocking library call, this doesn't apply, and the program is valid (assuming it meets all the other validity criteria).
Yes. From [intro.progress]
The implementation may assume that any thread will eventually do one of the following:
- terminate,
- make a call to a library I/O function,
- perform an access through a volatile glvalue, or
- perform a synchronization operation or an atomic operation.
[ Note: This is intended to allow compiler transformations such as removal of empty loops, even when termination cannot be proven. — end note ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With