I'm interfacing a C program (main()
is in C) with C++. At some points in my code I want to stop execution of the program. Now I'd like to know, how can I do this cleanly?
At the moment I call std::terminate()
but more out of a lack of better ideas. The main thing that bugs me isn't even that I'm not freeing the memory (because it's freed anyway on program termination, right?) but that the MSVS Just-in-time Debugger pops up and I get an ugly error message about terminating the runtime in an unusual way.
EDIT: As this has caused confusion: Returning from main()
with return 0
is not possible in this case.
The exit () function is used to break out of a loop. This function causes an immediate termination of the entire program done by the operation system. void exit (int code); The value of the code is returned to the calling process, which is done by an operation system.
Exit() is a core function in the C/C++ programming language that is used to instantly end the calling process (function). It is possible to call from any function. It informs the operating system of the state of program termination by passing an int value. It is usually used when software crashes unexpectedly.
exit functionh>, terminates a C++ program. The value supplied as an argument to exit is returned to the operating system as the program's return code or exit code. By convention, a return code of zero means that the program completed successfully.
If you concern about cleaning up and invoking destuctors then
exit(EXIT_SUCCESS); // or EXIT_FAILURE
is the best choice between exit
, terminate
and abort
.
Function exit
calls descructors and cleans up the automatic storage objects (The object which declared in the global scope). It also calls the functions passed to atexit
.
Function abort
is useful for abnormal exits and will not clean up anything. It doesn't call the functions passed to atexit
.
Function terminate
does not exist in C. It's useful when you have an exception and you can't handle it but finishing the program.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With