...
This noncompliant code example defines a function that is called before the program exits to clean up.
Code Block | ||||
---|---|---|---|---|
| ||||
void cleanup(void) { /* Delete temporary files, restore consistent state, etc. */ } int main(void) { if (atexit(cleanup) != 0) { /* Handle error */ } /* ... */ assert(/* something bad didn't happen */); /* ... */ } |
...
In this compliant solution, the call to assert()
is replaced with an if
statement that calls exit()
to ensure that the proper termination routines are run.
Code Block | ||||
---|---|---|---|---|
| ||||
void cleanup(void) { /* delete temporary files, restore consistent state, etc */ } int main(void) { if (atexit(cleanup) != 0) { /* Handle error */ } /* ... */ if (/* something bad happened */) { exit(EXIT_FAILURE); } /* ... */ } |
...