teaching machines

CS 330 Lecture 15 – Malloc

February 24, 2012 by . Filed under cs330, lectures, spring 2012.

Agenda

TODO

Code

makefile

EXES = first arrays months oob strings test_stack

all: $(EXES)

$(EXES): %: %.c stack.h stack.c
	gcc -o $@ -Wall -g -std=c99 $< stack.c

clean:
	rm -f $(EXES)

stack.h

#ifndef STACK_H
#define STACK_H

const int ESTACK_UNDERFLOW;

struct stack_t {
  int *elements;
  int top;
};

struct stack_t *make_stack(int nelements);

// Add element to stack
void push(struct stack_t *stack, int new_thing);

int pop(struct stack_t *stack);

int peek(const struct stack_t *stack);

void free_stack(struct stack_t *stack);

#endif

stack.c

#include <assert.h>
#include <errno.h>
#include <stdlib.h>

#include "stack.h"

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

const int ESTACK_UNDERFLOW = 10982;

/* ------------------------------------------------------------------------- */

struct stack_t *make_stack(int nelements) {
  /* struct stack_t *new_stack = (struct stack_t *) malloc(sizeof(struct stack_t)); */
  struct stack_t *new_stack = EZMALLOC(struct stack_t, 1);
  /* new_stack->elements = (int *) malloc(sizeof(int) * nelements); */
  new_stack->elements = EZMALLOC(int, nelements);
  new_stack->top = 0;
  return new_stack;
}

/* ------------------------------------------------------------------------- */

int peek(const struct stack_t *stack) {
  /* assert(stack->top > 0); */
  errno = ESTACK_UNDERFLOW;
  return stack->elements[stack->top - 1];
}

/* ------------------------------------------------------------------------- */

int pop(struct stack_t *stack) {
  /* assert(stack->top > 0); */
  errno = ESTACK_UNDERFLOW;
  --stack->top;
  return stack->elements[stack->top];
}

/* ------------------------------------------------------------------------- */

void push(struct stack_t *stack, int new_thing) {
  stack->elements[stack->top] = new_thing;
  stack->top++;
}

/* ------------------------------------------------------------------------- */

void free_stack(struct stack_t *stack) {
  free(stack->elements);
  free(stack);
}

/* ------------------------------------------------------------------------- */

test_stack.c

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "stack.h"
#include "foo.h"

int main(int argc, char **argv) {
  struct stack_t *operands = make_stack(100);

  for (int i = 0; i < argc; ++i) {
    if (isdigit(argv[i][0])) {
      push(operands, atoi(argv[i]));
    } else if (strcmp(argv[i], "*") == 0) {
      int b = pop(operands);
      int a = pop(operands);
      push(operands, a * b);
    } else if (strcmp(argv[i], "+") == 0) {
      int b = pop(operands);
      int a = pop(operands);
      push(operands, a + b);
    }
  }

  printf("%d\n", pop(operands));

  free_stack(operands);

  /* push(operands, 10); */
  /* push(operands, 13); */
  /* push(operands, 17); */

  /* for (int i = 0; i < 3; ++i) { */
    /* printf("pop(operands): %d\n", pop(operands)); */
  /* } */

  return 0;
}

Haiku

I’m glad I am young.
All the blech problems are solved.
What next? World peace.