E.6. Program Termination with exit and atexit

The general utilities library () 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.


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 
 4 using std::cout;
 5 using std::endl;
 6 using std::cin;
 7
 8 #include 
 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 << "
Enter 2 to terminate program normally
";
20
21 int answer;
22 cin >> answer;
23
24 // exit if answer is 1
25 if ( answer == 1 )
26 {
27 cout << "
Terminating program with function exit
";
28 exit( EXIT_SUCCESS );
29 } // end if
30
31 cout << "
Terminating 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
"
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
 


Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net