C Collections

cvector · Section 3

cvec_destroy

cvec_destroy - destroy a vector and clear the handle (type-inferred wrapper)

C Collections (libccollections, -lccollections)

#include <ccollections/cvector.h>
 
cvec_destroy(v);

Frees the vector's backing buffer and the container itself, then sets v to
NULL. A handle that is already NULL is left alone and nothing is freed for
it, so destroying the same variable a second time does nothing and is
safe.
 
Everything, the private copy of the procedure table included, is freed
through the memory management procedures that created the vector, which
is why the macro needs no allocator argument. This is the name used by the
type-inferred macro layer; it is cvector_destroy(3) under another name and
behaves the same way.
 
Only the handle is cleared. The companion type variables declared by
cvec_declare(3) and the construct macros stay in scope and stay valid, so
cvec_init(3) or cvec_init_mp(3) can give the same variable a new vector
later.

v  A cvec lvalue that names the vector to destroy, such as a variable or
   an array element, and that the macro must be able to assign to. It is
   evaluated exactly once, so an expression with a side effect, such as
   vs[i++], destroys and clears one element. The macro sets it to NULL
   before it returns and does nothing for an lvalue that holds NULL.

None. The macro expands to a statement, not to an expression.

If the vector holds pointers to heap buffers, the elements are not freed
one by one, so the caller has to free the element data first.
 
This call invalidates every pointer obtained earlier from
cvec_data_ptr(3) or cvec_at_ptr(3).
 
cvec_declare_scoped(3), cvec_construct_scoped(3) and
cvec_construct_mp_scoped(3) destroy their vector at the end of the scope
by themselves and clear the handle as they do it, so an explicit call
inside such a block is legal but not needed.

Destroy a vector once, through one handle. If you copy a cvec into a
second variable, that copy dangles after the destruction, and destroying
through it later is a double free.

cvec_construct(vec, int);
cvec_push(vec, 42);
 
cvec_destroy(vec);   /* vec is NULL after this call */
cvec_destroy(vec);   /* this call does nothing and is safe */

cvector_destroy(3), cvec_construct(3), cvec_init(3), cvec_declare_scoped(3), cvec_reset(3)