Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Changed compliant example to get & set safe values

...

However, this issue was discussed at the April 2008 meeting of ISO/IEC WG14 and it was agreed that there are no known implementations in which it would be an error to read a value to from a volatile static variable and the original intent of the committee was that both reading and writing variables of volatile sig_atomic_t would be strictly conforming.

...

Portably, signal handlers can only unconditionally get or set a flag of type volatile sig_atomic_t and return.

Code Block
bgColor#ccccff
#include <signal.h>
#include <stdlib.h>
#include <string.h>

volatile sig_atomic_t e_flag = 0;
volatile sig_atomic_t e_value = 1;

void handler(int signum) {
  e_flag = 1e_value;
}

int main(void) {
  char *err_msg;
  enum { MAX_MSG_SIZE = 24 };
  signal(SIGINT, handler);

  err_msg = (char *)malloc(MAX_MSG_SIZE);
  if (err_msg == NULL) {
    /* handle error condition */
  }

  strcpy(err_msg, "No errors yet.");

  /* main code loop */

  if (e_flag) {
    strcpy(err_msg, "SIGINT received.");
  }
  return 0;
}

...