10

what is the difference between exit and std::exit in C++? I have researched it but i couldn't find anything.

What is the difference between these two codes:

1:

if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
    std::cout << "Error: Can't initialize the SDL \n";
    exit(EXIT_FAILURE);
}

2:

if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
    std::cout << "Error: Can't initialize the SDL \n";
    std::exit(EXIT_FAILURE);
}
2
  • 1
    One lives in the std namespace, and the other does not. Which you get depends on the headers you include. Aug 2, 2016 at 15:34
  • Both are exactly the same. Aug 2, 2016 at 15:35

4 Answers 4

16

They're two names for the same function that does the same things.

Note, however, that in C++ std::exit/exit (regardless of how you get to its name) does have some behavior that's not specified for the exit in the C library. In particular,

  1. exit first destroys all objects with thread storage duration that are associated with the current thread.
  2. Objects with static storage duration are destroyed, and any functions registered with atexit are invoked.
    • If one of these throws an exception that isn't caught, terminate is invoked.
  3. After that we get the normal C behavior:
    • Open C streams are flushed if they have unwritten data, then they're closed.
    • Files created by calling tmpfile are removed.
    • Control is returned to the host environment, returning success or failure depending on the value passed in the call to exit (0 or EXIT_SUCCESS => success, EXIT_FAILURE => failure, anything else is implementation defined).

Note in particular that local objects are not destroyed by any call to exit.

That means, in effect, that you should really forget all of the above, and simply never call exit/std::exit from C++ code. While the committee apparently feels that compatibility with C code is a strong enough motivation that they need to leave it in the standard, you certainly don't need to use it--and under almost any reasonably normal circumstances, you shouldn't. Destroying local objects on exit from a scope is an important enough part of C++ that a function like exit that removes this guarantee leads to almost nothing but headaches.

If you need behavior vaguely similar to that of exit, you normally want to do something like this:

struct my_exit : public std::exception { 
    int value;
    my_exit(int value) : value(value) {}
};

int main() { 
    try {
        // do normal stuff
    }

    catch(my_exit const &e) {
        return e.value;
    }
}

Then in the rest of the code where you would otherwise have called exit, you instead throw my_exit(whatever_value);. This way, all the local variables will be destroyed (i.e., stack unwinding will happen) and then you'll do a normal exit to the environment.

8

exit (when using a C++ compiler) is "borrowed" from the C standard library via the header stdlib.h.

std::exit is the C++ standard library version; defined in cstdlib.

In C++ you ought to use the latter, but the two functions do exactly the same thing.

1

When you have declare before : using namespace std; there is no difference between std::exit and exit

This declaration permit to avoid the writing of the prefix std.

So you can also write cout instead of std::cout

1

There is no real difference. Unless you have different functions named exit in different scopes/namespaces (and if you do; well, I don't know what to say then - just don't), exit(), ::exit() and std::exit() is the same thing.

You usually don't want to call exit though, since doing so terminates the program without running local and global destructors (just atexit registered functions). Sometimes (rarely) that's what you want, but generally not - you want to return from main instead.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.