Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Added errno.h to compliant example, made example syntactically correct.

...

Code Block
bgColor#FFCCCC
langc
#include <string.h>
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdlib.h>
 
errno_t f(voidconst char *input_str) {
  size_t size = strlen(input_str)+1;
  char *c_str = (char *)malloc(size);
  memcpy(c_str, input_str, size);
  /* ... */
  free(c_str);
  c_str = NULL;
  /* ... */
  return 0;
}

Compliant Solution (POSIX)

...

Code Block
bgColor#ccccff
langc
#include <string.h>
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdlib.h>
#include <errno.h>
 
errno_t f(voidconst char *input_str) {
  size_t size = strlen(input_str)+1;
  char *c_str = (char *)malloc(size);
  if (NULL == c_str) {
    return ENOMEM; /* Indicate allocation failure */
  }
  memcpy(c_str, input_str, size);
  /* ... */
  free(c_str);
  c_str = NULL;

  /* ... */
  return 0;
}

This compliant solution is categorized as a POSIX solution because it returns ENOMEM, which is defined by POSIX but not by the C Standard.

...