Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > noreturn

Reply
Thread Tools

noreturn

 
 
Les Cargill
Guest
Posts: n/a
 
      08-18-2012
Ralph Spitzner wrote:
> Angel wrote:
>> On 2012-08-16, Bill Cunningham <(E-Mail Removed)> wrote:

> [....]
>> In the standard library, the functions exit() and abort() are examples
>> of functions that never return, since calling either ends the program.

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


Any "main" for a thread will be no-return, and will
be an "infinite loop".

> (Read return address gets pulled off the stack)
>
> Not that my 0.02EUR are worth anything, these days
>
>

--
Les Cargill
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      08-18-2012
Ralph Spitzner <(E-Mail Removed)> writes:
> Angel wrote:
>> On 2012-08-16, Bill Cunningham <(E-Mail Removed)> wrote:

> [....]
>> In the standard library, the functions exit() and abort() are examples
>> of functions that never return, since calling either ends the program.

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


"Return" does not just mean "terminate". The `noreturn` or
`_Noreturn` function specifier means that the function never executes
a `return` statement and never reaches its closing `}`. It's about the
C `return` statement, not about pulling the return address off the stack
(even if there is a stack).

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.

(Note: `...` is the Markdown syntax for code samples; see
<http://en.wikipedia.org/wiki/Markdown>. I'm going to start using it
here.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Ralph Spitzner
Guest
Posts: n/a
 
      08-19-2012
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']


--
See why I hate Windows users?
All pain, no gain.
-Howard Chu

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-19-2012
Ralph Spitzner <(E-Mail Removed)> writes:
> 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.


Yes, that would be a memory leak.

> So something has to watch over it, which will notice the
> program has 'returned/terminated'.


There's no requirement in the language for a mechanism to detect memory
leaks.

> [That's just my perception of a 'return']


The 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?

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Angel
Guest
Posts: n/a
 
      08-19-2012
On 2012-08-19, Ralph Spitzner <(E-Mail Removed)> wrote:
> 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'.


"noreturn" merely specifies that the function never returns to its
caller. It says nothing about whether the program keeps running after
that. The program could terminate immediately, like the exit() function
for example. It could be running indefinitely, for example the main
execution loop of a game. "noreturn" merely specifies that this function
will never execute a return statement or "fall off" at the end of its
definition.

> [That's just my perception of a 'return']


In C, the meaning of "return" is well defined and not subject to
personal perception.


--
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c
 
Reply With Quote
 
Ralph Spitzner
Guest
Posts: n/a
 
      08-19-2012
Keith Thompson wrote:
[...]
> The 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?
>

No, not arguing about the _Noreturn statement itself (which might be
making things easier for the optimizer), but rather about that a
function must end somehow, be it by a kill -9.
Unless you're running on bare metal something will catch that
termination and clean up,therefore having noticed a 'return'

Maybe that noreturn statement is just a little bit too abstract,
or the term is just strange to me.

The compiler could just grok it by seeing something like
for(;
or
while(1)





--
See why I hate Windows users?
All pain, no gain.
-Howard Chu

 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      08-19-2012
On Sat, 18 Aug 2012 20:41:30 +0200, Ralph Spitzner wrote:

>> In the standard library, the functions exit() and abort() are examples
>> of functions that never return, since calling either ends the program.

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

 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      08-19-2012
Ralph Spitzner wrote:
) Keith Thompson wrote:
) [...]
)> The 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?
)>
) No, not arguing about the _Noreturn statement itself (which might be
) making things easier for the optimizer), but rather about that a
) function must end somehow, be it by a kill -9.
) Unless you're running on bare metal something will catch that
) termination and clean up,therefore having noticed a 'return'

Again: that's not what "return" means! So stop playing Humpty-Dumpty.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      08-19-2012
On 19-Aug-12 12:02, Nobody wrote:
> On Sat, 18 Aug 2012 20:41:30 +0200, Ralph Spitzner wrote:
>
>>> In the standard library, the functions exit() and abort() are examples
>>> of functions that never return, since calling either ends the program.

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


AIUI, setjmp() always returns at least once--and sometimes more than once.

> and swapcontext().


I'm not familiar with that one.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
 
Reply With Quote
 
Les Cargill
Guest
Posts: n/a
 
      08-19-2012
Stephen Sprunk wrote:
> On 19-Aug-12 12:02, Nobody wrote:
>> On Sat, 18 Aug 2012 20:41:30 +0200, Ralph Spitzner wrote:
>>
>>>> In the standard library, the functions exit() and abort() are examples
>>>> of functions that never return, since calling either ends the program.
>>>
>>> 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()

>
> AIUI, setjmp() always returns at least once--and sometimes more than once.
>
>> and swapcontext().

>
> I'm not familiar with that one.
>
> S
>



http://pubs.opengroup.org/onlinepubs...context.h.html


--
Les Cargill
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments