Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between exit() and abort()?

People also ask

What is the difference between exit and abort?

The difference between exit and abort is that exit allows the C++ runtime termination processing to take place (global object destructors get called). abort terminates the program immediately. The abort function bypasses the normal destruction process for initialized global static objects.

What is difference between exit () atexit ()?

exit() vs _Exit() function in C and C++ So what is the feature of this function? The exit() function performs some cleaning before terminating the program. It clears the connection termination, buffer flushes etc. This _Exit() function does not clean anything.

What does abort () do?

In the C Programming Language, the abort function raises the SIGABRT signal, and causes abnormal program termination that returns an implementation defined code indicating unsuccessful termination.

What is exit () function in C?

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. The general form of the exit() function is as follows − void exit (int code);


abort() exits your program without calling functions registered using atexit() first, and without calling objects' destructors first. exit() does both before exiting your program. It does not call destructors for automatic objects though. So

A a;
void test() { 
    static A b;
    A c;
    exit(0);
}

Will destruct a and b properly, but will not call destructors of c. abort() wouldn't call destructors of neither objects. As this is unfortunate, the C++ Standard describes an alternative mechanism which ensures properly termination:

Objects with automatic storage duration are all destroyed in a program whose function main() contains no automatic objects and executes the call to exit(). Control can be transferred directly to such a main() by throwing an exception that is caught in main().

struct exit_exception { 
   int c; 
   exit_exception(int c):c(c) { } 
};

int main() {
    try {
        // put all code in here
    } catch(exit_exception& e) {
        exit(e.c);
    }
}

Instead of calling exit(), arrange that code throw exit_exception(exit_code); instead.


abort sends a SIGABRT signal, exit just closes the application performing normal cleanup.

You can handle an abort signal however you want, but the default behavior is to close the application as well with an error code.

abort will not perform object destruction of your static and global members, but exit will.

Of course though when the application is completely closed the operating system will free up any unfreed memory and other resources.

In both abort and exit program termination (assuming you didn't override the default behavior), the return code will be returned to the parent process that started your application.

See the following example:

SomeClassType someobject;

void myProgramIsTerminating1(void)
{
  cout<<"exit function 1"<<endl;
}

void myProgramIsTerminating2(void)
{
  cout<<"exit function 2"<<endl;
}

int main(int argc, char**argv)
{
  atexit (myProgramIsTerminating1);
  atexit (myProgramIsTerminating2);
  //abort();
  return 0;
}

Comments:

  • If abort is uncommented: nothing is printed and the destructor of someobject will not be called.

  • If abort is commented like above: someobject destructor will be called you will get the following output:

exit function 2
exit function 1


The following things happen when a program calls exit():

  • Functions registered by the atexit function are executed
  • All open streams are flushed and closed, files created by tmpfile are removed
  • The program terminates with the specified exit code to the host

The abort() function sends the SIGABRT signal to the current process, if it is not caught the program is terminated with no guarantee that open streams are flushed/closed or that temporary files created via tmpfile are removed, atexit registered functions are not called, and a non-zero exit status is returned to the host.


From the exit() manual page:

The exit() function causes normal process termination and the value of status & 0377 is returned to the parent.

From the abort() manual page:

The abort() first unblocks the SIGABRT signal, and then raises that signal for the calling process. This results in the abnormal termination of the process unless the SIGABRT signal is caught and the signal handler does not return.


abort sends the SIGABRT signal. abort does not return to the caller. The default handler for the SIGABRT signal closes the application. stdio file streams are flushed, then closed. Destructors for C++ class instances are not, however (not sure on this one -- perhaps results are undefined?).

exit has its own callbacks, set with atexit. If callbacks are specified (or only one), they are called in the order reverse of their registration order (like a stack), then the program exits. As with abort, exit does not return to the caller. stdio file streams are flushed, then closed. Also, destructors for C++ class instances are called.