How to exit a program with mixed C/C++ cleanly

c, c++

Solution

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.

Problem

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.

Original source