C++ cstdlib _Exit function


The C++ <cstdlib> _Exit function terminate the program normally and return the control to the host(OS).The declaration of the function is shown below.

void _Exit(int status) noexcept;

Parameters:
status -Value returned to the host after the program terminate.

Return type
void

Some points to note:

i) The ‘_Exit’ terminates the program without calling destructors for automatic objects,static storage or object storage having duration same as the current thread.

ii) It does not call the functions registered by ‘atexit’.

iii) The open stream with unwritten buffer are flushed or the open stream are closed and the temporary files are removed is implementation-defined.

iv) The status returned to the host environment operate in the same way as the ‘exit’ function.If the status is EXIT_SUCCESS or 0 a status successful termination is returned,if status is EXIT_FAILURE a status unsuccessful termination is returned.

Link :C++ cstdlib atexit

Code example

void func( ) { cout<<“func( ) called \n”; }

int main( )
{
static int i=90 ;

cout<<“atexit called \n”;
atexit( func );

_Exit( 0 );

cin.get( );
return 0;
}

Output,

atexit called

The func() is not called although it was registered and also note the storage of ‘i’ is not destroyed as it is of static type.

The difference between ‘exit’ and ‘_Exit’ is given below.






Difference between _Exit and exit functions

Both the functions ‘exit’ and ‘_Exit’ causes the normal termination of the program whenever it is called in the midway of the program or at the end of the program.But,however there exist some differences between them in their behavior and the underlying tasks they may or may not perform.

The first trivial difference between them which might be worth mentioning is ‘_Exit’ function is a new addition to the language in C++11.The ‘exit’ function on the other hand has been part of the language since the beginning and so it has undergone through many changes and evolution.Perhaps we may also see some changes in the behavior of ‘_Exit’ as the language advances more with the passage of time.

Link :C++ cstdlib exit

A more notable differences are given below.

Difference 1:At the ‘exit’ call the static storage and the objects storage with the current thread duration are destroyed. The ‘_Exit’ on the other hand does not perform the above task.The static storage are left undestroyed.
(**Note for both the function ‘exit()’ and ‘_Exit()’ ,the automatic objects are not destroyed)

Difference 2: The function registered by ‘atexit’ is called if the program terminate with the call to the ‘exit’.But the registered functions are not called for the ‘_Exit’ call.

Code example

#include <iostream>
#include <cstdlib>

using namespace std;

void func( ) { cout<<“func() called \n”; }

int main( )
{
atexit( func );

cout<<“Calling exit( 0 ) \n” ;
exit( 0 );

/*** try running this code **

cout<<“Calling _Exit( 0 ) \n” ;
_Exit( 0 );

***/

cin.get();
return 0;
}

Output when exit(0) is used

Calling exit( 0 )
func() called

output when _Exit(0) is used

Calling _Exit( 0 )

Difference 3: The ‘exit’ call close the C type open stream and the unwritten buffer are flushed and the temporary file created using ‘tmpfile’ is removed.However,these may not happen at ‘_Exit’ call.

Code example

#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

int main( )
{
FILE *fp;

fp=fopen( “text.txt” , “w” ); ///test if the file is open

exit( 0 );

cin.get();
return 0;
}

When ‘exit’ is call the open file ‘text.txt’ will be closed appropriately,but this will not happen if ‘_Exit’ is used.