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

Started by
11 comments, last by Adam Hamilton 18 years, 1 month ago
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.
-Mike
Advertisement
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().
--== discman1028 ==--
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 :)

This topic is closed to new replies.

Advertisement