Example of Preparation of a Routine for Termination Processing Registration and Execution (atexit) |
The method for preparation of the library function atexit to register termination processing is described.
The atexit function registers, in a table for termination processing, a function address passed as a parameter. If the number of functions registered exceeds the limit (in this case, the number that can be registered is assumed to be 32), or if an attempt is made to register the same function twice, NULL is returned. Otherwise, a value other than NULL (in this case, the address of the registered function) is returned.
A program example is shown below.
The method for preparation of an exit library function for program termination is described. Program termination processing will differ among user systems; refer to the program example below when preparing a termination procedure according to the specifications of the user system.
The exit function performs termination processing for a program according to the termination code for the program passed as a parameter, and returns to the environment in which the program was started. Here, the termination code is set to an external variable, and execution returned to the environment saved by the setjmp function immediately before the main function was called. In order to return to the environment prior to program execution, the following callmain function should be created, and instead of calling the function main from the PowerOn_Reset_PC initial setting function, the callmain function should be called.
A program example is shown below.
On abnormal termination, processing for abnormal termination must be executed in accordance with the specifications of the user system.
In a C++ program, the abort function will also be called in the following cases:
Below is shown an example of a program which outputs a message to the standard output device, then closes all files and begins an infinite loop to wait for reset.