alloc.h: Memory allocation

The C malloc/calloc may return NULL to indicate out-of-memory. We would rather have functions that, if they return at all, guarantee to have allocated what’s requested. * Also, convenient to have specific-typed versions of calloc for allocating arrays.

void *surely_malloc(size_t); // Guarantees to allocate, if it returns at all
double *double_calloc(int n);  // Allocate and zero an array of n doubles
int *int_calloc(int n);     // Allocate and zero an array of n ints

void double_clear(double *p, int n);    // Set an array of n doubles to zeros

alloc.c: Memory allocation

void *surely_malloc(size_t n) {
  void *p = malloc(n);
  if (!p) exit(1);
  if (0) free(p); // temporary hack to make sure free is referenced
  return p;
}

void double_clear(double *p, int n) {
  for (int i=0; i<n; i++) 
     p[i]=0.0;
}

double *double_calloc(int n) {
  double *p = (double*)surely_malloc(n*sizeof(double));
  double_clear(p, n);
  return p;
}

int *int_calloc(int n) {
  int *p = (int*)surely_malloc(n*sizeof(int));
  for (int i=0; i<n; i++) 
     p[i]=0;
  return p;
}