vt_resize 3 2016-02-28 genvector manual


vt_resize - resize a vector to the size specified


int vt_resize(vt_t *vect, vt_size_t new_size)


NOTE: user code normally does not need to call vt_resize directly, but should call vt_truncate or vt_enlarge(); for details, see "shortcomings" below.

Resize (grow or shrink) a vector, low level. If new_size is smaller than the current size, elements beyond new_size are discarded (optionally deconstructed). If new_size is larger than the current size, new elements are allocated, optionally initialized (but are not constructed); the actual size of the of the vector does not grow, but the allocation does so that writes below the new size will not cause a realloc.

If new_size is zero, vt_uninit() is performed: all elements are discarded (optionally deconstructed) and the result is an empty vector with no array allocation. This happens even if the vector is terminated (see vt_term(7)).

Shortcomings: vt_resize primarily operates on the alloced field of the vector and changes the used field only as a side effect (when shrinking). Furthermore vt_resize never writes a terminator. For terminated vectors, this results in bad or missing termination on shrink.

Note: vt_t and the vt_ prefix are type-dependant, see vt_t(7).

vect A vector already initialized using vt_init.
new_size The new allocated size of the vector.


Returns 0 on success or -1 on error. Error conditions include allocation failure or constructor failure (if constructor is enabled for the type).


vt_resize 3 2016-02-28 genvector manual