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

Started by
11 comments, last by Adam Hamilton 18 years, 1 month ago
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?
n/a
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)

Beginner in Game Development?  Read here. And read here.

 

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).
thank u
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.
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,
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.

--== discman1028 ==--
The correct value to normally return on error is EXIT_FAILURE, in stdlib.h/cstdlib
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
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().
thank u
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.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement