Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a program that never terminates a valid C++ program?

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.

like image 794
bolov Avatar asked Nov 05 '19 09:11

bolov


People also ask

What is the termination of C?

Software Engineering C Some of the common ways to terminate a program in C are: exit. _Exit() quick_exit.

How do you stop a program in C?

So the C family has three ways to end the program: exit(), return, and final closing brace.

What is the function called to terminate a program?

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.


2 Answers

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).

like image 137
Daniel H Avatar answered Oct 17 '22 06:10

Daniel H


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 ]

like image 22
Caleth Avatar answered Oct 17 '22 07:10

Caleth