eteran/c-vector
FreeA dynamic array implementation in C similar to the one found in standard C++
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
Pros & Cons
- 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
- 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