teaching machines

CS 330 Lecture 13 – Data Structures in C, Make

February 20, 2013 by . Filed under cs330, lectures, spring 2013.

Agenda

Variable-length Arrays

The C99 standard allows arrays whose sizes are only known at runtime to be created on the stack. However, it may not be a good idea to make dynamically-sized arrays in this way:

Questions

Code

audioh.c

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define ERROR(msg) \
  printf("%s at line %d, file %s.\n", msg, __LINE__, __FILE__); \
  exit(1);

void error(const char *msg) {
  printf("%s at line %d, file %s.\n", msg, __LINE__, __FILE__);
  exit(1);
}

#define EZMALLOC(type, n) \
  (type *) malloc((n) * sizeof(type))

int main(int argc, char **argv) {

  int nsamples = atoi(argv[1]);
  float divisors[] = {
    atof(argv[2]),
    atof(argv[3])
  };

  short *samples = EZMALLOC(short, nsamples * 2);

  /* samples[0]; // first sample of left channel */
  /* samples[1]; // first sample of right channel */

  /* samples[i * 2 + 0]; // ith sample of left channel? */
  /* samples[i * 2 + 1]; // ith sample of left channel? */

  int i;
  float *sample = samples;
  for (i = 0; i < nsamples; ++i) {
    /* samples[i * 2 + 0] = (short) (sin(i / divisors[0]) * 32767); */
    /* samples[i * 2 + 1] = (short) (sin(i / divisors[1]) * 32767); */
    sample[0] = (short) (sin(i / divisors[0]) * 32767);
    sample[1] = (short) (sin(i / divisors[1]) * 32767);
    sample += 2;
  }

  FILE *file = fopen("samples.raw", "w");
  fwrite(samples, sizeof(short), nsamples * 2, file);
  fclose(file);

  free(samples);

  return 0;
}

stack.c

#define EZMALLOC(type, n) \
  (type *) malloc((n) * sizeof(type))

struct stack_t {
  int size;
  int capacity;
  int *items;
};

typedef struct stack_t stack_t;

stack_t *make_stack(int capacity) {
  stack_t *stack = EZMALLOC(stack_t, 1);  
  stack->size = 0;
  stack->capacity = capacity;
  stack->items = EZMALLOC(int, stack->capacity);
}

void stack_push(stack_t *stack, int item) {
  if (stack->size >= stack->capacity) {
    fprintf(stderr, "Stack full, you foo bag.\n");
    exit(1);
  }

  stack->items[stack->size] = item;
  ++stack->size;
}

int stack_pop(stack_t *stack) {
  if (stack->size == 0) {
    fprintf(stderr, "Stack empty, you foo bag.\n");
    exit(1);
  }

  int item = stack->items[stack->size - 1];
  --stack->size;
  return item;
}

TODO

Haiku

They’re all powerups.
Every program I write.
Each takes me farther.