Dynarr 0.0.1
C dynamic array
|
In this section we'll discuss creation of the dynamic array.
Based on Example file.
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.
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:
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 :
Create dynarr specifying dynarr_opts_t::ext_header_size to reserve space for ext_t
.
Initialize extended header:
With dynarr_clone you are able to produce exact copy of an existing array.
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.
Other option is to just use an assert
. If allocation error is unlikely to happen, but you still need to indicate if it occures.
Advantage of asserts if that they can be disabled by providing -DNDEBUG
compilation flag.
Prevent memory leaks by deallocation resources, when they not needed anymore!
dynarr
created with dynarr_create or dynarr_clone !