924 reputation
157
bio website cs.wisc.edu/~rkennedy
location Twin Cities, Minn.
age 34
visits member for 3 years, 9 months
seen Jul 16 at 11:47

Oct
20
awarded  Yearling
Feb
25
awarded  Caucus
Oct
20
awarded  Yearling
Oct
20
awarded  Yearling
Jun
24
awarded  Great Answer
Jun
23
awarded  Citizen Patrol
Jun
21
comment Why should main() be short?
Yes, @Martin, if you called main recursively, then your C++ program was ill-formed, so it wasn't a valid test. As far as I can tell, implementations may indeed simply call exit(main()) — since programs aren't allowed to call main themselves (like your test did), the implementation doesn't really need to worry about inserting special code in the function epilogue just to guarantee that returning from it acts like exit.
Jun
21
comment Why should main() be short?
C is a little different from C++ in that regard, @Martin. C allows recursive calls to main, so it says that returning from the initial call to main is the same as calling exit. In the scenario we're imagining here, there are two main functions: the host application's, and the one you import from a library. Calling the imported function would be the initial call of that function, so returning from it should terminate your program. (I doubt how many implementations actually work that way, though, since they probably don't expect to export main anyway.)
Jun
21
awarded  Commentator
Jun
21
comment Why should main() be short?
@Coder, if returning from main does not have the effect of calling exit on your compiler, then your compiler doesn't follow the standard. That the standard dictates such behavior for main proves that there is something special about it. The special thing about main is that returning from it has the effect of calling exit. (How it does that is up to compiler writers. The compiler could simply insert code in the function epilogue that destroys static objects, calls atexit routines, flushes files, and terminates the program — which, again, isn't something you want in a library.)
Jun
21
comment Why should main() be short?
@Coder, whether exit leaves main is irrelevant. We're not discussing the behavior of exit. We're discussing the behavior of main. And the behavior of main includes the behavior of exit, whatever that may be. That's what makes it undesirable to import and call main (if doing such a thing is even possible or allowed).
Jun
21
comment Why should main() be short?
Now you're doing it wrong, @Coder. Your object has automatic storage duration. The standard says objects with static storage duration are destroyed, so it wasn't a valid test. Your conclusion does not follow from your test. Besides, all you can hope to learn from your test is whether your compiler follows the standard.
Jun
21
comment Why should main() be short?
@Coder, see C++03 §3.6.1/5: "A return statement in main has the effect of leaving the main function … and calling exit with the return value as the argument." Also see §18.3/8, which explains that "objects with static storage duration are destroyed" and "all open C streams … are flushed" when you call exit. C99 has similar language.
Jun
21
awarded  Guru
Jun
20
awarded  Good Answer
Jun
20
awarded  Mortarboard
Jun
20
awarded  Nice Answer
Jun
20
awarded  Teacher
Jun
20
comment Why should main() be short?
I suppose it might be more accurate to say you can't import it, @Shoosh. The C++ standard forbids calling it from your own code. Besides, its linkage is implementation-defined. Also, returning from main effectively calls exit, which you usually don't want a library to do.
Jun
20
answered Why should main() be short?