...
Code Block | ||
---|---|---|
| ||
#include <stdio.h> #include <stdlib.h> void exit1(void) { if(expr) exitif(<expr>); { //clean up and ready program for closing* ...cleanup code... */ exit(0); ...} } int main (void) { atexit(exit1); /* ... program //program code code... */ exit(); return 00); } |
Compliant Code
To have functionality where the program can quit from within a function registered by at_exit() it is necessary to use a function used for abnormal termination such as _Exit() or abort().
...
The function _exit terminates the calling process "immediately". Any open file descriptors belonging to the process are closed; any children of the process are inherited by process 1,
init
, and the process's parent is sent aSIGCHLD
signal. The value status is returned to the parent process as the process's exit status, and can be collected using one of the wait family of calls. The function_Exit
is equivalent to_exit
.
Code Block | ||
---|---|---|
| ||
#include <stdio.h> #include <stdlib.h> #include <stdio.h> #include <stdlib.h> void exit1(void) { if(expr) _Exit();if(<expr>) { //clean up and ready program for closing* ...cleanup code... */ _Exit(0); ...} } int main (void) { atexit(exit1); /* ...program //program codecode... */ exit(0); return 0; } |
The call to _Exit()
will immediately terminate the program and no undefined behavior will happen like in the non compliant example.
...
Wiki Markup |
---|
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 7.20.4.3, "The {{exit}} function" |