Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #161489 > unrolled thread
| Started by | Mehdi Amini <atorrses@gmail.com> |
|---|---|
| First post | 2021-06-25 10:46 +0430 |
| Last post | 2021-06-25 14:51 -0400 |
| Articles | 10 on this page of 30 — 13 participants |
Back to article view | Back to comp.lang.c
Calling exit in main Mehdi Amini <atorrses@gmail.com> - 2021-06-25 10:46 +0430
Re: Calling exit in main Kli-Kla-Klawitter <kliklaklawitter69@gmail.com> - 2021-06-25 08:19 +0200
Re: Calling exit in main Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-06-25 00:48 -0700
Re: Calling exit in main Kli-Kla-Klawitter <kliklaklawitter69@gmail.com> - 2021-06-25 10:08 +0200
Re: Calling exit in main Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-06-25 01:38 -0700
Re: Calling exit in main Kli-Kla-Klawitter <kliklaklawitter69@gmail.com> - 2021-06-25 11:03 +0200
Re: Calling exit in main Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-06-25 08:27 -0700
Re: Calling exit in main Kli-Kla-Klawitter <kliklaklawitter69@gmail.com> - 2021-06-25 18:52 +0200
Re: Calling exit in main Kaz Kylheku <563-365-8930@kylheku.com> - 2021-06-25 17:14 +0000
Re: Calling exit in main scott@slp53.sl.home (Scott Lurndal) - 2021-06-25 18:17 +0000
Re: Calling exit in main Kaz Kylheku <563-365-8930@kylheku.com> - 2021-06-25 18:48 +0000
Re: Calling exit in main Real Troll <real.troll@trolls.com> - 2021-06-25 20:21 +0100
Re: Calling exit in main Kli-Kla-Klawitter <kliklaklawitter69@gmail.com> - 2021-06-26 09:21 +0200
Re: Calling exit in main Kaz Kylheku <563-365-8930@kylheku.com> - 2021-06-28 16:45 +0000
Re: Calling exit in main Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-06-28 18:14 +0100
Re: Calling exit in main Richard Damon <Richard@Damon-Family.org> - 2021-06-25 13:33 -0400
Re: Calling exit in main Kaz Kylheku <563-365-8930@kylheku.com> - 2021-06-25 15:05 +0000
Re: Calling exit in main Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-06-25 01:34 -0700
Re: Calling exit in main James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-06-25 11:51 -0400
Re: Calling exit in main Kaz Kylheku <563-365-8930@kylheku.com> - 2021-06-25 16:13 +0000
Re: Calling exit in main Guillaume <message@bottle.org> - 2021-06-25 18:48 +0200
Re: Calling exit in main Kaz Kylheku <563-365-8930@kylheku.com> - 2021-06-25 17:05 +0000
Re: Calling exit in main Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2021-06-25 15:22 -0600
Re: Calling exit in main Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2021-06-25 15:29 -0600
Re: Calling exit in main James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-06-25 14:50 -0400
Re: Calling exit in main James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-06-25 14:59 -0400
Re: Calling exit in main Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-06-25 13:40 -0700
Re: Calling exit in main Jens Schweikhardt <usenet@schweikhardt.net> - 2021-06-26 08:25 +0000
Re: Calling exit in main Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-06-25 10:42 -0700
Re: Calling exit in main James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-06-25 14:51 -0400
Page 2 of 2 — ← Prev page 1 [2]
| From | Guillaume <message@bottle.org> |
|---|---|
| Date | 2021-06-25 18:48 +0200 |
| Message-ID | <sb51ds$1hr3$1@gioia.aioe.org> |
| In reply to | #161498 |
Le 25/06/2021 à 17:51, James Kuyper a écrit : > I think "return 0;" is conceptually simpler, but keep in mind that > > "a return from the initial call to the main function is equivalent to > calling the exit function with the value returned by the main function > as its argument;" (5.1.2.2.3) > > Therefore, the net effect is almost the same. Sorry, but I'm not sure I understand the "almost" here. According to the standard, that you quoted, both are supposed to be equivalent. At least on a conforming implementation. If, on some implementation, they are actually not equivalent, then wouldn't this be non-conforming? Lastly, maybe we need to know what exactly "equivalent" means in this context. Is the term properly defined in the standard? (Because for all I know, you seem to be assuming here that "equivalent" could imply "almost the same" - almost meaning: not quite.)
[toc] | [prev] | [next] | [standalone]
| From | Kaz Kylheku <563-365-8930@kylheku.com> |
|---|---|
| Date | 2021-06-25 17:05 +0000 |
| Message-ID | <20210625100354.785@kylheku.com> |
| In reply to | #161500 |
On 2021-06-25, Guillaume <message@bottle.org> wrote: > Le 25/06/2021 à 17:51, James Kuyper a écrit : >> I think "return 0;" is conceptually simpler, but keep in mind that >> >> "a return from the initial call to the main function is equivalent to >> calling the exit function with the value returned by the main function >> as its argument;" (5.1.2.2.3) >> >> Therefore, the net effect is almost the same. > > Sorry, but I'm not sure I understand the "almost" here. > > According to the standard, that you quoted, both are supposed to be > equivalent. At least on a conforming implementation. Think about it: how can the total sum of all the effects of a return statement and of a function call be 100% the same? The return statement terminates a function, causing all of its non-static local variables to become indeterminate. A function call suspends the caller, while another function executes. The caller's automatic storage remainds valid while it is suspended.
[toc] | [prev] | [next] | [standalone]
| From | Joe Pfeiffer <pfeiffer@cs.nmsu.edu> |
|---|---|
| Date | 2021-06-25 15:22 -0600 |
| Message-ID | <1bzgvdy707.fsf@pfeifferfamily.net> |
| In reply to | #161503 |
Kaz Kylheku <563-365-8930@kylheku.com> writes: > On 2021-06-25, Guillaume <message@bottle.org> wrote: >> Le 25/06/2021 à 17:51, James Kuyper a écrit : >>> I think "return 0;" is conceptually simpler, but keep in mind that >>> >>> "a return from the initial call to the main function is equivalent to >>> calling the exit function with the value returned by the main function >>> as its argument;" (5.1.2.2.3) >>> >>> Therefore, the net effect is almost the same. >> >> Sorry, but I'm not sure I understand the "almost" here. >> >> According to the standard, that you quoted, both are supposed to be >> equivalent. At least on a conforming implementation. > > Think about it: how can the total sum of all the effects of a return > statement and of a function call be 100% the same? > > The return statement terminates a function, causing all of its > non-static local variables to become indeterminate. > > A function call suspends the caller, while another function executes. > The caller's automatic storage remainds valid while it is suspended. That's true in general, but in this case we're talking specifically about returning from main() vs calling exit(). When you've returned from main() the local variables don't exist any more; the call to exit() does the same (you never return back to main() from exit()). So I'm also not clear on how the net effect is only "almost" the same.
[toc] | [prev] | [next] | [standalone]
| From | Joe Pfeiffer <pfeiffer@cs.nmsu.edu> |
|---|---|
| Date | 2021-06-25 15:29 -0600 |
| Message-ID | <1bv961y6nx.fsf@pfeifferfamily.net> |
| In reply to | #161514 |
Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes: > Kaz Kylheku <563-365-8930@kylheku.com> writes: > >> On 2021-06-25, Guillaume <message@bottle.org> wrote: >>> Le 25/06/2021 à 17:51, James Kuyper a écrit : >>>> I think "return 0;" is conceptually simpler, but keep in mind that >>>> >>>> "a return from the initial call to the main function is equivalent to >>>> calling the exit function with the value returned by the main function >>>> as its argument;" (5.1.2.2.3) >>>> >>>> Therefore, the net effect is almost the same. >>> >>> Sorry, but I'm not sure I understand the "almost" here. >>> >>> According to the standard, that you quoted, both are supposed to be >>> equivalent. At least on a conforming implementation. >> >> Think about it: how can the total sum of all the effects of a return >> statement and of a function call be 100% the same? >> >> The return statement terminates a function, causing all of its >> non-static local variables to become indeterminate. >> >> A function call suspends the caller, while another function executes. >> The caller's automatic storage remainds valid while it is suspended. > > That's true in general, but in this case we're talking specifically > about returning from main() vs calling exit(). When you've returned > from main() the local variables don't exist any more; the call to exit() > does the same (you never return back to main() from exit()). So I'm > also not clear on how the net effect is only "almost" the same. (following up myself) Ah, OK -- the examples of passing pointers to local variables to functions called at exit show the distinction.
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@alumni.caltech.edu> |
|---|---|
| Date | 2021-06-25 14:50 -0400 |
| Message-ID | <sb58ib$nj4$1@dont-email.me> |
| In reply to | #161500 |
On 6/25/21 12:48 PM, Guillaume wrote:
> Le 25/06/2021 à 17:51, James Kuyper a écrit :
>> I think "return 0;" is conceptually simpler, but keep in mind that
>>
>> "a return from the initial call to the main function is equivalent to
>> calling the exit function with the value returned by the main function
>> as its argument;" (5.1.2.2.3)
>>
>> Therefore, the net effect is almost the same.
>
> Sorry, but I'm not sure I understand the "almost" here.
>
> According to the standard, that you quoted, both are supposed to be
> equivalent. At least on a conforming implementation.
>
> If, on some implementation, they are actually not equivalent, then
> wouldn't this be non-conforming?
>
> Lastly, maybe we need to know what exactly "equivalent" means in this
> context. Is the term properly defined in the standard? (Because for all
> I know, you seem to be assuming here that "equivalent" could imply
> "almost the same" - almost meaning: not quite.)
I think the equivalence described in 5.1.2.2.3 is exact; the "almost" in
my statement is a consequence of that exact equivalence, not a result of
thinking it might be less than exact.
There's an important difference between calling exit() from inside of
main(), and calling exit() after main() has returned, using the value
returned by main(): all objects with automatic storage duration declared
inside of main() that are in scope at the point where the return
statement gets executed end their lifetime as soon as it gets executed,
before any exit handlers have a chance to execute. If a function has
been registered with atexit() that refers to such an object, that
function will have undefined behavior. That would not be the case if
exit() were called inside of main().
Referring to such an object from within an exit handler is little
tricky, but far from impossible. Here's the simplest example I could
think of:
int *pint;
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@alumni.caltech.edu> |
|---|---|
| Date | 2021-06-25 14:59 -0400 |
| Message-ID | <sb592j$3a0$1@dont-email.me> |
| In reply to | #161509 |
On 6/25/21 2:50 PM, James Kuyper wrote:
> On 6/25/21 12:48 PM, Guillaume wrote:
>> Le 25/06/2021 à 17:51, James Kuyper a écrit :
>>> I think "return 0;" is conceptually simpler, but keep in mind that
>>>
>>> "a return from the initial call to the main function is equivalent to
>>> calling the exit function with the value returned by the main function
>>> as its argument;" (5.1.2.2.3)
>>>
>>> Therefore, the net effect is almost the same.
>>
>> Sorry, but I'm not sure I understand the "almost" here.
>>
>> According to the standard, that you quoted, both are supposed to be
>> equivalent. At least on a conforming implementation.
>>
>> If, on some implementation, they are actually not equivalent, then
>> wouldn't this be non-conforming?
>>
>> Lastly, maybe we need to know what exactly "equivalent" means in this
>> context. Is the term properly defined in the standard? (Because for all
>> I know, you seem to be assuming here that "equivalent" could imply
>> "almost the same" - almost meaning: not quite.)
>
> I think the equivalence described in 5.1.2.2.3 is exact; the "almost" in
> my statement is a consequence of that exact equivalence, not a result of
> thinking it might be less than exact.
>
> There's an important difference between calling exit() from inside of
> main(), and calling exit() after main() has returned, using the value
> returned by main(): all objects with automatic storage duration declared
> inside of main() that are in scope at the point where the return
> statement gets executed end their lifetime as soon as it gets executed,
> before any exit handlers have a chance to execute. If a function has
> been registered with atexit() that refers to such an object, that
> function will have undefined behavior. That would not be the case if
> exit() were called inside of main().
>
> Referring to such an object from within an exit handler is little
> tricky, but far from impossible. Here's the simplest example I could
> think of:
>
> int *pint;
I accidentally sent this message first as a Thunderbird "Reply" to
Guillaume's personal e-mail. When I corrected that mistake by re-sending
it as a "Followup", I accidentally dropped the rest of my example:
void exit_handler(void) {
// Do something with *pint
}
int main(void) {
int i;
pint = &i;
atexit(exit_handler);
exit(0);
}
I think it makes a lot of sense for Thunderbird to distinguish between
"Reply" and "Followup" - they should have done so from the beginning.
However, I've spent more than a decade getting used to hitting "Reply"
when I wanted to post a Followup, and it's proving very difficult for me
to break that habit.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2021-06-25 13:40 -0700 |
| Message-ID | <87o8btu18y.fsf@nosuchdomain.example.com> |
| In reply to | #161500 |
Guillaume <message@bottle.org> writes:
> Le 25/06/2021 à 17:51, James Kuyper a écrit :
>> I think "return 0;" is conceptually simpler, but keep in mind that
>> "a return from the initial call to the main function is equivalent
>> to
>> calling the exit function with the value returned by the main function
>> as its argument;" (5.1.2.2.3)
>> Therefore, the net effect is almost the same.
>
> Sorry, but I'm not sure I understand the "almost" here.
>
> According to the standard, that you quoted, both are supposed to be
> equivalent. At least on a conforming implementation.
>
> If, on some implementation, they are actually not equivalent, then
> wouldn't this be non-conforming?
That statement in the standard isn't 100% accurate, but there is a
clarifying footnote.
N1570 5.1.2.2.3 says:
If the return type of the main function is a type compatible
with int, a return from the initial call to the main function is
equivalent to calling the exit function with the value returned
by the main function as its argument; 11) reaching the } that
terminates the main function returns a value of 0. If the return
type is not compatible with int, the termination status returned
to the host environment is unspecified.
Footnote 11 says:
In accordance with 6.2.4, the lifetimes of objects with automatic
storage duration declared in main will have ended in the former
case, even where they would not have in the latter.
> Lastly, maybe we need to know what exactly "equivalent" means in this
> context. Is the term properly defined in the standard? (Because for
> all I know, you seem to be assuming here that "equivalent" could imply
> "almost the same" - almost meaning: not quite.)
I don't think the intent is that "equivalent" means something other than
"equivalent". It's just a (fairly minor) imprecision in the wording.
Here's a contrived program that illustrates the difference:
#include <stdio.h>
#include <stdlib.h>
int *ptr;
static void one_last_thing(void) {
printf("The answer is %d\n", *ptr);
}
int main(void) {
int answer = 42;
ptr = &answer;
atexit(one_last_thing);
#ifdef UNSAFE
return 0;
#else
exit(0);
#endif
}
If the program does a `return 0;`, then the lifetime of `answer` ends
when the return statement is executed, *before* one_last_thing() is
called. Thus evaluating the expression `*ptr` has undefined behavior.
(When I try it, it prints "The answer is 0".)
If the program calls `exit(0);`, then one_last_thing is called
(indirectly) from within exit, which was called from within main,
so main's automatic storage still exists.
And if you write code like this for any reason other than to demonstrate
this issue, we will all point and laugh at you.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */
[toc] | [prev] | [next] | [standalone]
| From | Jens Schweikhardt <usenet@schweikhardt.net> |
|---|---|
| Date | 2021-06-26 08:25 +0000 |
| Message-ID | <ijo6kkFolftU1@mid.individual.net> |
| In reply to | #161500 |
Guillaume <message@bottle.org> wrote in <sb51ds$1hr3$1@gioia.aioe.org>: # Le 25/06/2021 à 17:51, James Kuyper a écrit : #> I think "return 0;" is conceptually simpler, but keep in mind that #> #> "a return from the initial call to the main function is equivalent to #> calling the exit function with the value returned by the main function #> as its argument;" (5.1.2.2.3) #> #> Therefore, the net effect is almost the same. # # Sorry, but I'm not sure I understand the "almost" here. # # According to the standard, that you quoted, both are supposed to be # equivalent. At least on a conforming implementation. You have to read very carefully and mind every word. Did you perhaps gloss over the word "initial"? The main function may be called recursively. In that case the difference between return and exit() should be clear: returning from a non-initial call continues execution in the caller, while exit terminates the program. Regards, Jens -- Jens Schweikhardt http://www.schweikhardt.net/ SIGSIG -- signature too long (core dumped)
[toc] | [prev] | [next] | [standalone]
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2021-06-25 10:42 -0700 |
| Message-ID | <sb54i4$d7c$1@dont-email.me> |
| In reply to | #161489 |
On 6/24/2021 11:16 PM, Mehdi Amini wrote:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void)
> {
> printf("Calling exit in main\n");
> exit (0);
> }
>
> Both gcc and clang issue no diagnostic about it.
What would they issue diagnostic about? Where do you see a formal reason
for that here?
> Do you think it is proper to use exit instead of return in main ?
It is ugly, i.e. stylistically bad, if the intent is simply to replace
`return 0`. Otherwise it is virtually the same.
--
Best regards,
Andrey Tarasevich
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@alumni.caltech.edu> |
|---|---|
| Date | 2021-06-25 14:51 -0400 |
| Message-ID | <sb58jv$nj4$2@dont-email.me> |
| In reply to | #161506 |
On 6/25/21 1:42 PM, Andrey Tarasevich wrote:
> On 6/24/2021 11:16 PM, Mehdi Amini wrote:
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> int main(void)
>> {
>> printf("Calling exit in main\n");
>> exit (0);
>> }
>>
>> Both gcc and clang issue no diagnostic about it.
>
> What would they issue diagnostic about? Where do you see a formal reason
> for that here?
They could issue a diagnostic about it being ugly, as you yourself
describe it below. It's not exactly unusual for compilers to issue
diagnostics about matters of style. I don't think this particular issue
is one that would justify such a diagnostic, but it's also not
particularly unusual for programmers to have differences of opinion
about how important any particular stylistic issue is. If anything, such
differences are the norm, not the exception.
>> Do you think it is proper to use exit instead of return in main ?
>
> It is ugly, i.e. stylistically bad, if the intent is simply to replace
> `return 0`. Otherwise it is virtually the same.
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.c
csiph-web