Dynarr 0.0.1
C dynamic array
Loading...
Searching...
No Matches
📘 Guide

🔨 Creating Dynarr

In this section we'll discuss creation of the dynamic array.
Based on Example file.

⚙️ With default options

A standard way to create dynarr_t is to call a macro wrapper dynarr_create, it passes default option values, so you dont have to provide them yourself.

dynarr_t *dynarr = dynarr_create(.element_size = sizeof(int));
#define dynarr_create(...)
Dynarr constructor.
Definition dynarr.h:81

⚙️ With custom options

dynarr_t *dynarr = dynarr_create
(
.element_size = sizeof(float),
.initial_cap = 100,
.grow_factor = 1.5f,
.grow_threshold = 0.8,
.shrink_threshold = 0.2
);


⚙️ Without macro wrapper

You can avoid macro wrapper with default values if you know what you are doing.
Just create opts on stack and pass by reference into a dynarr_create_ function directly:

dynarr_opts_t opts = {.element_size = sizeof(int)};
Dynarr creating options.
Definition dynarr.h:25
size_t element_size
Definition dynarr.h:28
dynarr_t *dynarr = dynarr_create_(&opts);
dynarr_t * dynarr_create_(const dynarr_opts_t *const opts)
Constructor of the dynamic array.
Definition dynarr.c:89


⚙️ Allocate extended header

This is important for derived classes that need to include their data
in a memory allocated by underlying vector.

Define a structure ext_t that will be peallocated right after dynarr_header_t :

typedef struct
{
size_t meta;
// ...
// ...
// ...
}
ext_t;

Create dynarr specifying dynarr_opts_t::ext_header_size to reserve space for ext_t.

dynarr_t *dynarr = dynarr_create
(
.ext_header_size = sizeof(ext_t),
.element_size = sizeof(long)
);

Initialize extended header:

ext_t *header = dynarr_get_ext_header(dynarr);
*header = (ext_t) {0};
void * dynarr_get_ext_header(const dynarr_t *const dynarr)
Retrieve a location of extended header.
Definition dynarr.c:121


⿻ Clone an existing array

With dynarr_clone you are able to produce exact copy of an existing array.

dynarr_t *clone = dynarr_clone(dynarr);
dynarr_t * dynarr_clone(const dynarr_t *const dynarr)
Duplicate a dynarr.
Definition dynarr.c:129


❌ Error handling

Default implementation uses heap as allocation memory source. Depending on system architecture heap allocation is not fail proof, take it into your considerations.

Check that dynarr value is NULL, then resolve allocation error. If you got none resolution choises, perform gracefull program termination. See following example, print error and exit abnormally.

if (!dynarr)
{
perror("dynarr_create");
abort();
}


Other option is to just use an assert. If allocation error is unlikely to happen, but you still need to indicate if it occures.

assert(dynarr && "Buy more RAM!");

Advantage of asserts if that they can be disabled by providing -DNDEBUG compilation flag.


🧨 Deallocating Dynarr

Prevent memory leaks by deallocation resources, when they not needed anymore!

dynarr_destroy(dynarr);
void dynarr_destroy(dynarr_t *const dynarr)
Deallocates a dynamic array.
Definition dynarr.c:136
Attention
Always remember to deallocate dynarr created with dynarr_create or dynarr_clone !



⬆️ Extending a dynarr

Follow same guidelines as for the Vector
Extending a vector