Vector 0.0.2
Loading...
Searching...
No Matches
create.c
Different ways of managing vector's lifetime.
Author
Evgeni Semenov
#include "vector.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
void default_create(void);
void explicit_opts_create(void);
void extended_header(void);
void cloning(void);
int main(void)
{
default_create();
explicit_opts_create();
extended_header();
cloning();
return 0;
}
void default_create(void)
{
vector_t *vector = vector_create(.element_size = sizeof(int));
if (!vector)
{
perror("vector_create");
abort();
}
// ...
vector_destroy(vector);
}
void explicit_opts_create(void)
{
vector_t *vector = vector_create (
.element_size = sizeof(float),
.initial_cap = 100
);
assert(vector && "Buy more RAM!");
// ...
vector_destroy(vector);
}
void extended_header(void)
{
typedef struct
{
size_t meta;
// ...
// ...
// ...
}
ext_t;
vector_t *vector = vector_create (
.ext_header_size = sizeof(ext_t),
.element_size = sizeof(long)
);
// ...
ext_t *header = vector_get_ext_header(vector);
*header = (ext_t) {0};
// ...
vector_destroy(vector);
}
void cloning(void)
{
vector_opts_t opts = {.element_size = sizeof(int)};
vector_t *vector = vector_create_(&opts);
vector_t *clone = vector_clone(vector);
vector_destroy(vector);
}
void * vector_get_ext_header(const vector_t *const vector)
Provides a location where user can put a header for the derived class.
Definition vector.c:162
#define vector_create(...)
Vector constructor.
Definition vector.h:162
vector_t * vector_clone(const vector_t *const vector)
Duplicates a vector.
Definition vector.c:119
void vector_destroy(vector_t *const vector)
Deallocates vector.
Definition vector.c:112
vector_t * vector_create_(const vector_opts_t *const opts)
Vector contructor.
Definition vector.c:82
Vector options.
Definition vector.h:52
size_t element_size
Size of the underling element type.
Definition vector.h:56
Vector control structure type.
Definition vector.c:24
Public interface of the vector.