views:

2411

answers:

8

How do I quit a C++ program. Which function is called to end a program and which values does the method take?

To clarify I want to exit a C++ program from within my code. And I may want to exit the program outside of the main function of this program.

+16  A: 

Call the exit function.

Otávio Décio
Which objects' destructors get called when you call that function?
Rob Kennedy
exit() does not return. So no stack unwinding can take place. Not even global objects do not get destroyed. But the functions registered with atexit() will be called.
Martin York
A: 

You haven't really provided enough information on what type of program (console, windows, etc).

Generally, the program exits when the main() function exits.

EricLaw -MSFT-
A: 

Either return a value from your main or use the exit function. Both take an int. It doesn't really matter what value you return unless you have an external process watching for the return value.

Goz
+1  A: 

Generally you would use the exit() method with an appropriate exit status.

Zero would mean a successful run. A non-zero status indicates some sort of problem has occurred. This exit code is used by parent processes (e.g. shell scripts) to determine if a process has run successfully.

Brian Agnew
+4  A: 

The program will terminate when the execution flow reaches the end of the main function.

To terminate it before then, you can use the exit(int status) function, where status is a value returned to whatever started the program. 0 normally indicates a non-error state

chrisbunney
+8  A: 

As Martin York mentioned, exit doesn't perform necesaary clean-up like return does.

It's always better to use return in the place of exit. Incase if you are not in main, wherever you would like to exit the program, return to main first.

Consider the below example. With the following program, a file will be created with the content mentioned. But if return is commented & uncommented exit(0), the compiler doesn't assure you that the file will have the required text.

int main()
{
    ofstream os("out.txt");
    os << "Hello, Can you see me!\n";
    return(0);
    //exit(0);
}

Not just this, Having multliple exit points in a program will make debugging harder. Use exit only when it can be justified.

Narendra N
What do you recommend to achieve this behavior in a little bit bigger program? How do you always return to main cleanly if somewhere deeper in the code an error condition is triggered that should exit the program?
Janusz
Narendra N
+2  A: 

@Janusz If you have an error somewhere deep in the code, then either throw an exception or set the error code. It's always better to throw an exception instead of setting error codes.

Jagannath
+3  A: 

People are saying "call exit(return code)," but this is bad form. In small programs it is fine, but there are a number of issues with this:

  1. You will end up having multiple exit points from the program
  2. It makes code more convoluted (like using goto)
  3. It cannot release memory allocated at runtime

Really, the only time you should exit the problem is with this line in main.cpp:

return 0;

If you are using exit() to handle errors, you should learn about exceptions (and nesting exceptions), as a much more elegant and safe method.

Hooked
In a multi-threaded environment, an exception thrown in a different thread will not be handled though main() -- some manual cross-thread communication is needed before the subordinate thread expires.
Steve Gilham