Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Some errors, such as a value out of range, might be the result of erroneous user input. If the input program is interactive, the program it can prompt the user for an acceptable value. With other errors, such as a resource allocation failure, the system may have little choice other but to shut down.

...

Code Block
bgColor#ccccff
#include <stdlib.h>
/* ... */

if (/* something really bad happened */) {
  _Exit(EXIT_FAILURE);
}

The _exit() function is a synonym an alias for _Exit().

abort()

The quickest and most abrupt way to terminate a program, abort() takes no arguments and always signifies abnormal termination to the operating system.

...

The abort() function should not be called if it is important to perform application-specific cleanup before exiting. In this noncompliant code example, abort() is called after data is sent to an open file descriptor. The data may or may not actually get be written to the file.

Code Block
bgColor#FFCCCC
#include <stdlib.h>
#include <stdio.h>

int write_data(void) {
  const char *filename = "hello.txt";
  FILE *f = fopen(filename, "w");
  if (f == NULL) {
    /* Handle error */
  }
  fprintf(f, "Hello, World\n");
  /* ... */
  abort(); /* oops! data might not getbe written! */
  /* ... */
  return 0;
}

int main(void) {
  write_data();
  return 0;
}

...

For more details on proper usage of abort(), see ERR06-C. Understand the termination behavior of assert() and abort().

Risk

...

Assessment

For example, using Using abort() or _Exit() in place of exit() may leave written files in an inconsistent state, and may also leave sensitive temporary files on the file system.

...