www.gibmonks.com

Main Page

Previous Page
Next Page

[Page 1255 (continued)]

E.6. Program Termination with exit and atexit

The general utilities library (<cstdlib>) provides methods of terminating program execution other than a conventional return from function main. Function exit forces a program to terminate as if it executed normally. The function often is used to terminate a program when an error is detected in the input or if a file to be processed by the program cannot be opened.

Function atexit registers a function in the program to be called when the program terminates by reaching the end of main or when exit is invoked. Function atexit takes a pointer to a function (i.e., the function name) as an argument. Functions called at program termination cannot have arguments and cannot return a value.

Function exit takes one argument. The argument is normally the symbolic constant EXIT_SUCCESS or EXIT_FAILURE. If exit is called with EXIT_SUCCESS, the implementation-defined value for successful termination is returned to the calling environment. If exit is called with EXIT_FAILURE, the implementation-defined value for unsuccessful termination is returned. When function exit is invoked, any functions previously registered with atexit are invoked in the reverse order of their registration, all streams associated with the program are flushed and closed, and control returns to the host environment. Figure E.4 tests functions exit and atexit. The program prompts the user to determine whether the program should be terminated with exit or by reaching the end of main. Note that function print is executed at program termination in each case.


[Page 1256]

Figure E.4. Using functions exit and atexit.
(This item is displayed on pages 1256 - 1257 in the print version)

 1  // Fig. E.4: figE_04.cpp
 2  // Using the exit and atexit functions
 3  #include <iostream>
 4  using std::cout;
 5  using std::endl;
 6  using std::cin;
 7
 8  #include <cstdlib>
 9  using std::atexit;
10  using std::exit;
11
12  void print();
13
14  int main()
15  {
16     atexit( print ); // register function print
17
18     cout << "Enter 1 to terminate program with function exit"
19          << "\nEnter 2 to terminate program normally\n";
20
21     int answer;
22     cin >> answer;
23
24     // exit if answer is 1
25     if ( answer == 1 )
26     {
27        cout << "\nTerminating program with function exit\n";
28        exit( EXIT_SUCCESS );
29     }  // end if
30
31     cout << "\nTerminating program by reaching the end of main"
32          << endl;
33
34     return 0;
35  }  // end main
36
37  // display message before termination                         
38  void print()                                                  
39  {                                                             
40     cout << "Executing function print at program termination\n"
41          << "Program terminated" << endl;                      
42  }  // end function print                                      

  Enter 1 to terminate program with function exit
  Enter 2 to terminate program normally
  2

  Terminating program by reaching the end of main
  Executing function print at program termination
  Program terminated

  Enter 1 to terminate program with function exit
  Enter 2 to terminate program normally
  1

  Terminating program with function exit
  Executing function print at program termination
  Program terminated



Previous Page
Next Page