C Collections

cvector · Section 3

cvector_append_array

cvector_append_array - append an array of elements to a vector

C Collections (libccollections, -lccollections)

#include <ccollections/cvector.h>
 
bool cvector_append_array(cvec v, const void *arr_ptr, size_t elem_count);

Appends an array of elements to the end of the vector, reserving more
capacity when the vector needs room for all of them.

v           Vector to append to
arr_ptr     Pointer to the array of elements. The function only reads
            through it, so it can point at a const array.
elem_count  Number of elements in the array

true on success.
false when:
  - the new total count overflows or is more than ccol_max_elem_count
  - the memory allocation fails
  - arr_ptr aliases the backing buffer of v and elem_count reads past
    the end of that buffer's reservation

Either all the elements are appended or none are. The elements are copied
into the vector, and more capacity is reserved automatically when needed.
The function asserts when v is NULL.
 
arr_ptr can safely point into the backing buffer of v, for example a
pointer from cvector_data_ptr(3) or cvector_at(3); a capacity growth
started by this call doesn't invalidate it.
 
The source and destination ranges may overlap, for example when arr_ptr
aliases the buffer of v and elem_count reads past the current element
count into capacity that the vector has already reserved. The copy is safe
in that case, but a source range that aliases must stay inside the
reserved capacity of v. The one case where the call returns false and
leaves v unchanged for this reason is an arr_ptr into the backing buffer
of v with an elem_count that reads past the end of that buffer's
reservation. Only an aliasing source range can be checked this way,
because a caller's own array has no extent that the function can see, as
with any C array passed as a bare pointer.
 
The cvec_append_array(3) macro evaluates elem_count exactly once, on the
failure path too, so an expression with a side effect is safe to pass.

cvec_append_array(3), cvector_append_cvector(3)