#include #include "vec.h" /* The full definition of the struct, private to this file*/ struct vector { int* data; int max; int size; }; /* Constructor */ struct vector* vector_create(int init_size) { struct vector* v = malloc(sizeof(struct vector)); /* Make sure to ALWAYS give every piece of allocted memory a known starting value */ v->data = calloc(init_size, sizeof(int)); /* calloc clears to zeores */ v->max = init_size; v->size = 0; return v; } /* Member function (method) */ void vector_push_back(struct vector* me, int val) { /* Make sure to ALWAYS notice if parameters have insane values */ if ( me->size >= me->max) abort(); me->data[me->size++] = val; } /* A private helper function, static makes it local to this compilation unit */ static int* index_ref(struct vector* me, int index) { return &me->data[index]; } /* Member function (method) */ int vector_at(struct vector* me, int index) { /* Make sure to ALWAYS notice if parameters have insane values */ if ( index < 0 || index >= me->size) abort(); return *index_ref(me, index); } /* Destructor */ void vector_delete(struct vector* me) { free(me->data); free(me); }