Generic Containers in C: vec
Martin Uecker, 2025-07-20
I discuss the implementation of type and bounds safe generic containers in C. Previously, I discussed a span type, and bounds checking using arrays.
Here, I will discuss a vector type. A vector type is essentially a resizable array. A vector type could be used as in the following example.
int main() { vec(int) *vec_ptr = calloc(1, sizeof *vec_ptr); if (!vec_ptr) // memory out abort(); for (int i = 0; i < 10; i++) vec_push(int, &vec_ptr, i); int sum = sum_vec(vec_ptr); free(vec_ptr); printf("sum: %d
", sum); }
In C, it is actually fairly easy to define a vector type.
... continue reading