Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: changed final CS to an exception

...

Code Block
bgColor#ccccff
langc
#include <stdlib.h>

const size_t BUFFER_SIZE = 32;

int f(void) {
  char *text_buffer = (char *)malloc(BUFFER_SIZE); 
  if (text_buffer == NULL) {
    return -1;
  }
 
  free(text_buffer);
  return 0;
}

Compliant Solution (static storage duration)

Exceptions

MEM31-EX1: It is permissible not to free memory that is used throughout the lifetime of the program. The following code example illustrates a pointer In this compliant solution, the pointer object that stores the return value from malloc() that is stored in a static variable of static storage duration.

Code Block
bgColor#ccccff
langc
#include <stdlib.h>
 
const size_t BUFFER_SIZE = 32;

int f(void) {
  static char *text_buffer = NULL;
  if (text_buffer == NULL) {
    text_buffer = (char *)malloc(BUFFER_SIZE); 
    if (text_buffer == NULL) {
      return -1;
    }
  }
  return 0;
}

...