Should I use exit() or just return statements in main()? Personally I favor the 'return' statements 'cause I feel it's like reading any other function and the flow control when I'm reading the code is smooth (in my opinion). And even if I want to refactor the main() function, having 'return' seems like a better choice than exit().
Does exit() do anything special that 'return' doesn't?
Thanks for your time!
-
In C,
return
inmain
will return to the_start
function in libc which will callmain
and after that invokesexit
system call, so there's no special difference. I preferreturn
as it's more stylish.FreeMemory : This isn't entirely correct when dealing with C++. Or rather, your comment is correct, but incomplete.Mehrdad Afshari : @FreeMemory True, I edited my comment to specify "In C," explicitly.brone : Nit-pick alert, but this is target-specific -- there is no requirement that there be anything, let alone with a particular name, that calls main. The compiler seems free to treat returns from main as special (according to my reading of the standard anyway).Mehrdad Afshari : Yes, it was just an example, but it's nevertheless the same, whatever the name of _start be, and even if the main would be a special function, somehow the compiler should inject the exit after it. It doesn't make much difference here though. -
I always use
return
because the standard prototype formain()
says that it does return anint
.That said, some versions of the standards give
main
special treatment and assume that it returns 0 if there's no explicitreturn
statement. Given the following code:int foo() {} int main(int argc, char *argv[]) {}
G++ only generates a warning for
foo()
and ignores the missing return frommain
:% g++ -Wall -c foo.cc foo.cc: In function ‘int foo()’: foo.cc:1: warning: control reaches end of non-void function
Jason Baker : I don't know about C, but the C++ standard specifies that if you don't return a value in main, it's assumed to return 0.Jason Baker : It seems as though C99 is the same: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?id=1043284376&answer=1044841143d0k : C99 and C++ return 0 if there is no return statement, C90 doesn't. -
Even though there is not much of a difference, i strongly prefer return to keep the natural flow of an application.
-
Actually, there is a difference, but it's subtle. It has more implications for C++, but the differences are important.
When I call
return
inmain()
, destructors will be called for my locally scoped objects. If I callexit()
, no destructor will be called for my locally scoped objects! Re-read that.exit()
does not return. That means that once I call it, there are "no backsies." Any objects that you've created in that function will not be destroyed. Often this has no implications, but sometimes it does, like closing files (surely you want all your data flushed to disk?).Note that
static
objects will be cleaned up even if you callexit()
. Finally note, that if you useabort()
, no objects will be destroyed. That is, no global objects, no static objects and no local objects will have their destructors called.Proceed with caution when favoring exit over return.
http://groups.google.com/group/gnu.gcc.help/msg/8348c50030cfd15a
Arkadiy : abort() exits with error condition (non-zero exit code) and may be even a core. If you need to exit w/o calling static destructors, use _exit .FreeMemory : @Arkadiy **Absolutely.** abort signals some error condition. And, stylistically, I usually also use exit() to signal an error condition, and return 0 from main to indicate normal termination.Mike Koval : From the exit() manpage: 2. Flush all open output streams. It sounds like any open output streams would be flushes regardless of whether the appropriate destructor is called. -
Another difference:
exit
is a Standard Library function so you need to include headers and link with the standard library. To illustrate (in C++), this is a valid program:int main() { return 0; }
but to use
exit
you'll need an include:#include <cstdlib> int main() { exit( EXIT_SUCCESS ); }
Plus this adds an additional assumption: that calling
exit
frommain
has the same side effects as returning zero. As others have pointed out, this depends on what kind of executable you're building (i.e., who's callingmain
). Are you coding an app that uses the C-runtime? A Maya plugin? A Windows service? A driver? Each case will require research to see ifexit
is equivalent toreturn
. IMHO usingexit
when you really meanreturn
just makes the code more confusing. OTOH, if you really do meanexit
, then by all means use it.
0 comments:
Post a Comment