Jump to content
  • Advertisement
Sign in to follow this  
MasterDario

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

This topic is 4456 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
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
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!