B
Bill Cunningham
What does this new noreturn specifer do? Is it related to return?
Bill
Bill
What does this new noreturn specifer do? Is it related to return?
Angel said:In a way yes. It tells the compiler that this function is not
supposed to return at all. This allows the compiler to make certain
optimizations which it couldn't make if the function should be
expected to return.
In the standard library, the functions exit() and abort() are examples
of functions that never return, since calling either ends the program.
A function that is marked as noreturn should of course never contain a
return statement, nor should it be allowed to "fall off" at the end of
the function definition.
In the standard library, the functions exit() and abort() are examples of
functions that never return, since calling either ends the program.
Bill Cunningham said:What does this new noreturn specifer do? Is it related to return?
Keith Thompson said:Did you read something that mentions the "noreturn" specifier without
explaining what it means?
If so, the latest C11 draft is at
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
If not, just keep reading.
So should void always be a function's type with this? Do we just
basically have a new specifier here?
On 2012-08-16, Bill Cunningham <[email protected]> wrote: > Angel wrote: > >> In a way yes. It tells the compiler that this function is not >> supposed to return at all. This allows the compiler to make certain >> optimizations which it couldn't make if the function should be >> expected to return. >> >> In the standard library, the functions exit() and abort() are examples >> of functions that never return, since calling either ends the program. >> >> A function that is marked as noreturn should of course never contain a >> return statement, nor should it be allowed to "fall off" at the end of >> the function definition. > > So should void always be a function's type with this? Do we just > basically have a new specifier here? I couldn't find anything that requires the return type to be void, but since the function is not supposed to return at all, a return type of void seems the most logical choice to me. -- "C provides a programmer with more than enoughrope to hang himself. C++ provides a firing squad, blindfold and last cigarette." - seen in comp.lang.c
Any function that calls a noreturn function, where the call is not inside
a conditional block, also never return.
Most X/Motif programs never return from main() - they call XtAppMainLoop(), which never returns. So should maiin() in this case be apscified as noreturn?
IF so, should it still have a return at the end? If there is no return statement at the end of a non-void noreturn function, will the compiler refrain from complaining about the missing return?
Angel said:In the standard library, the functions exit() and abort() are examples
of functions that never return, since calling either ends the program.
Ralph said:Hmm, as far as I see it, the only function which does actually
_never_ return to anything is the main loop of the OS.
All others are subject to garbage collection and heap cleanup.
Therefore they have to return to _something_.
Ralph Spitzner said:Hmm, as far as I see it, the only function which does actually
_never_ return to anything is the main loop of the OS.
All others are subject to garbage collection and heap cleanup.
Therefore they have to return to _something_.
(Read return address gets pulled off the stack)
Not that my 0.02EUR are worth anything, these days
For example, this function:
_Noreturn void func(void) {
while (1) {
do_something();
sleep(1);
}
}
will terminate when the program is terminated, but it never returns to
its caller, so the `_Noreturn` is appropriate.
Ralph Spitzner said:Keith Thompson wrote:
[...]For example, this function:
_Noreturn void func(void) {
while (1) {
do_something();
sleep(1);
}
}
will terminate when the program is terminated, but it never returns to
its caller, so the `_Noreturn` is appropriate.
Yes, but that do_something() might actually do something
like allocating memory an possibly forgetting to free() it.
So something has to watch over it, which will notice the
program has 'returned/terminated'.
[That's just my perception of a 'return']
Keith Thompson wrote:
[...]For example, this function:
_Noreturn void func(void) {
while (1) {
do_something();
sleep(1);
}
}
will terminate when the program is terminated, but it never returns to
its caller, so the `_Noreturn` is appropriate.
Yes, but that do_something() might actually do something
like allocating memory an possibly forgetting to free() it.
So something has to watch over it, which will notice the
program has 'returned/terminated'.
[That's just my perception of a 'return']
No, not arguing about the _Noreturn statement itself (which might beThe meaning of 'return' isn't a matter of perception.
Are you arguing that the use of `_Noreturn` in my example is incorrect,
or that it should mean something other than what the standard says it
means?
Hmm, as far as I see it, the only function which does actually
_never_ return to anything is the main loop of the OS.
The issue is whether it actually *returns* to its caller. Other examples
of functions which don't return are longjmp() and execve(). Grey areas
include setjmp()
and swapcontext().
Stephen said:AIUI, setjmp() always returns at least once--and sometimes more than once.
I'm not familiar with that one.
S
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.