eteran/c-vector logo

eteran/c-vector

Free

A dynamic array implementation in C similar to the one found in standard C++

FreeFree tier
Type
Open Source

About eteran/c-vector

c-vector is a header-only C library (C89 compatible) that provides a type-safe dynamic array implementation inspired by C++'s std::vector. It uses a memory layout where metadata (size, capacity, and an optional destructor function pointer) is stored in extra memory allocated before the user-facing data pointer. The library is implemented entirely as macros, making it easy to integrate into any C project without external dependencies. It supports common vector operations including push_back, pop_back, erase, iteration via begin/end, random access, and freeing. The growth policy can be configured: by default capacity doubles when full, or by defining CVECTOR_LINEAR_GROWTH the vector grows by one element at a time to minimize wasted space.

Key Features

Type-safe dynamic array (vector) implementation in plain C89
Header-only library implemented entirely as macros
Metadata stored in extra allocated memory before the data pointer (size, capacity, destructor)
Supports push_back, pop_back, erase, size, capacity, begin/end iterators, front, back, at, and free
Configurable growth policy: linear (one element at a time) or exponential (doubling) via #define
Compatible with any data type (int, float, struct, etc.)
Minimal overhead: 2 * sizeof(size_t) + sizeof(void (*)(void *)) per vector

Pros & Cons

Pros
  • Type-safe due to macro-based implementation (compiler checks types at instantiation)
  • Minimal memory overhead compared to many generic container libraries
  • Familiar API for developers coming from C++
  • C89 compatible and header-only, no build system changes required
  • Optional linear growth mode to avoid over-allocation
Cons
  • No built-in element destructor calls (user must handle cleanup manually before free)
  • All-macro implementation can lead to code bloat if used extensively in multiple translation units
  • Metadata stored before the user pointer may be opaque to debuggers and static analysis tools
  • No built-in sorting, searching, or other higher-level algorithms; only basic vector operations provided

Best For

Embedded systems requiring a dynamic array in C without external dependenciesC projects needing a simple, familiar vector API similar to C++ std::vectorLightweight dynamic array for educational purposes or small utilitiesReplacement for manual realloc-based dynamic arrays with type safety

FAQ

What C standard does c-vector require?
c-vector is written in C89 (ANSI C) and should compile with any conforming C compiler.
How is memory overhead calculated?
The total overhead per vector is 2 * sizeof(size_t) for size and capacity, plus sizeof(void (*)(void *)) for an optional destructor pointer.
How does the growth policy work?
By default, the vector doubles its capacity each time it runs out of space. If the CVECTOR_LINEAR_GROWTH macro is defined before including cvector.h, the capacity increases by exactly one element each time, minimizing unused memory.
Can I use c-vector with custom structs?
Yes, the vector can hold any type. Simply declare a pointer of your desired type (e.g., MyStruct *v = NULL) and use the same macro-based operations; the library handles the memory allocation.
Does c-vector call destructors when freeing elements?
No. The library stores an optional destructor function pointer but does not call it automatically. The user must handle any cleanup before calling cvector_free.