3 ms·
> This is a corner of C I'm not familiar with, but does the standard say anything about functions that never return? A few things, but not a lot of detail. >
by shakna 7y ago
> This is a corner of C I'm not familiar with, but does the standard say anything about functions that never return?
A few things, but not a lot of detail.
> A function declared with a_Noreturn function specifier shall not return to its caller 6.7.4-8
It is recommended, but not enforced, that the implementation warns if this might not be the case.
> The implementation should produce a diagnostic message for a function declared with a_Noreturn function specifier that appears to be capable of returning to its caller 6.7.4-9
They do provide some example code that explicitly says that non-returning functions, that are marked as such, need to be explicit that they don't return. (As C implicitly attempts to return if given the opportunity). Not doing so is Undefined Behaviour.
_Noreturn void f () {
abort(); //ok
}
_Noreturn void g (int i) {
//causes undefined behavior if i<=0
if (i > 0) abort();
}
So a compiler should be able to assume that a _Noreturn function either loops forever or terminates. Anything else would appear to be UB.
The compiler can't assume that a function always returns.