Versions Compared

Key

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

...

Consider a multithreaded application that encounters an error while calling a system function. The strerror() function returns a human-readable error string given an error number. The C Standard, Section 7.24.6.2 [ISO/IEC 9899:2011], specifically states that strerror() is not required to avoid data races. Conventionally, it could rely on a static array that maps error numbers to error strings, and that array might be accessible and modifiable by other threads. (This code is specific to POSIX because C99 and C11 do not guarantee that fopen() is not guaranteed to will set errno if an error occurs in C99 or C11.)

Code Block
bgColor#FFCCCC
langc
errno = 0;
FILE* fd = fopen(filename, "r");
if (fd == NULL) {
  char* errmsg = strerror(errno);
  printf("Could not open file because of %s\n", errmsg);
}

...