Sign in to follow this  
MasterDario

Whats the difference between return -1 and exit(1) in c++?

Recommended Posts

MasterDario    120
I've seen these two lines many times in c++ programs. I've seen return -1 in some file i/o error checking and exit(1) when trying to divide by 0. Is there a major difference between these two?

Share this post


Link to post
Share on other sites
Alpha_ProgDes    6936
return -1 is user defined (I believe). It's just there to let the programmer know that something went wrong.

exit(1) is a C library command. you use when you want to shut down the program completely after an error. (*actually let me get back to you on that)

Share this post


Link to post
Share on other sites
wrice    140
exit(1) is also able to be user defined code number.

Usualy, exit(0) is normal exit code, but -1 or 1 is abnormal code.

If you call return -1 on main() function, it's same with calling exit(-1).

Share this post


Link to post
Share on other sites
z80    134
return is used to exit from a function optionally returning a value (if the function is not declared as void).

exit() however terminates the whole application returning an exitcode to the O/S, so they are very different.

If you have a standard int main(int argc, char *argv[]) function in your application return and exit() within this function may seem similar since doing a return here also terminates the application.

Share this post


Link to post
Share on other sites
Not exactly. exit() is a function that never returns. It means that if you have to execute some non trivial destructors in main(), they won't be executed because nou never return from main(). At least, that's what I recall from it (see the man page says).

Beside that, the result from the program caller point of view is exactly the same :)

Regards,

Share this post


Link to post
Share on other sites
discman1028    212
I believe, in Unix, that exit() differs somewhat from return in main(), when you're trying to deal with "zombie processes," I forget exactly how. If this is of concern, I can look further.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
The correct value to normally return on error is EXIT_FAILURE, in stdlib.h/cstdlib

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by Anonymous Poster
The correct value to normally return on error is EXIT_FAILURE, in stdlib.h/cstdlib


Only if the programmer doesn't want to use different exit values for indicating different errors. If the program will be used for scripting then returning an exact error code is important. The standard specifies the 0 ... 255 range as valid return codes, with 0 being the indicator of a successful run.

Viktor

Share this post


Link to post
Share on other sites
wrice    140
Quote:
Original post by discman1028
I believe, in Unix, that exit() differs somewhat from return in main(), when you're trying to deal with "zombie processes," I forget exactly how. If this is of concern, I can look further.


It's wrong.

In UNIX, neighter exit() nor return in main() don't work about any zombie processes.

If you want to handle zombies, you have to use wait().

Share this post


Link to post
Share on other sites
Bregma    9214
Quote:
Original post by Emmanuel Deloget
Not exactly. exit() is a function that never returns. It means that if you have to execute some non trivial destructors in main(), they won't be executed because nou never return from main(). At least, that's what I recall from it (see the man page says).

Beside that, the result from the program caller point of view is exactly the same :)


Yes, that's the important difference between exit() and return from main().

Beware, through, that although destructors for objects of automatic storage duration in main() are not called, destructors for objects of static storgae duration declared at namespace level will still be destroyed properly. The heirarchy is something like this.

(1) return from main() (including "falling off the bottom," which is an implicit return(0) in C++), destroys all object in main and all global objects correctly

(2) exit(), does not destroy objects in main, destroys global objects

(3) abort(), does not destroy objects in main or global objects, goes directly to jail, does not collect $200.

Choose your poison.

Share this post


Link to post
Share on other sites
Anon Mike    1098
The actual value (-1 or 1 or whatever) does not have a deep significant meaning. Whoever started your app can look at that value and do something with it but it's app-specific what exactly it does, if anything.

The convention is to return 0 if everything went ok but that is not universal. e.g. a word counting program might return the number of words it counted.

Share this post


Link to post
Share on other sites
discman1028    212
Quote:
Original post by wrice
Quote:
Original post by discman1028
I believe, in Unix, that exit() differs somewhat from return in main(), when you're trying to deal with "zombie processes," I forget exactly how. If this is of concern, I can look further.


It's wrong.

In UNIX, neighter exit() nor return in main() don't work about any zombie processes.

If you want to handle zombies, you have to use wait().


Right, but I think "exit()" may inform wait().

Share this post


Link to post
Share on other sites
Adam Hamilton    271
If you had the time and wanted to see for your self what it does you could always write a couple of simple programs

#include <iostream>

int main(int argc, char** argv)
{
std::cout << "Hello" << std::endl;
return 1;
}

and

#include <iostream>

int main(int argc, char** argv)
{
std::cout << "Hello" << std::endl;
exit(1);
}


Switch debugging information on and be sure to have the CRT source installed otherwise you will be hassled by the debuggereand step through line by line even when the line gets to the curly brace step into it (You can step over the cout function call, it is only there to demonstrate the destructor call at the end)

Alternatively you could browse the CRT source that came with C++ and look for the line that calls _main and look at the _exit function

Happy code surfing :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this