C++ cstdlib quick_exit

The cstdlib quick_exit calls the functions registered by ‘at_quick_exit’ when the program terminates.

void quick_exit(int status) noexcept;

status -The code returned to the host environment -the OS- to signify the status of the call ,whether the call succeeded or failed.

Return type

Some points to note:

i) In calling the ‘quick_exit’ the functions registered by ‘at_quick_exit’ are called in the reverse order of their registration.

ii) The objects shall not be destroyed in calling the ‘quick_exit’ function.One of the side effect of this is the destructor is never call.

iii) If the control leaves the registered function at ‘quick_exit’ call because the function cannot handle the exception thrown then ‘terminate()’ shall be called.

iv) After calling the registered functions, ‘quick_exit’ shall call the function ‘_Exit(status)’.

v) The standard file buffers are not flushed in calling the function.

Link: C++ cstdlib at_quick_exit

Code example

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

int main( )
at_quick_exit( func );

quick_exit( 0 );

cin.get( );
return 0;


func called

‘func()’ is registered by ‘at_quick_exit’ by passing func() pointer as it’s argument,hence in calling the ‘quick_exit’ function func() is called.


A more exhaustive code example is provided below.

class A
A() { cout<<“A constructor”; }

static void sfunc() { cout<<“sfunc called”; }

~A() { cout<<“A destructor”; }

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

int main( )
A a ;

at_quick_exit( a.sfunc );

at_quick_exit( ofunc );

quick_exit( 0 );

cin.get( );
return 0;


A constructor
ofunc called
sfunc called

The ‘A’ class destructor is never called.Also afunc() is called first because it is registered later comparing to the ‘sfunc()’ function.


Stop wasting time,earn money($$$) from your website-Join Now!