A lot of C projects end up implementing a vector-like API. Dynamic arrays are such a common need, that it's nice to abstract away the memory management as much as possible. A typical C implementation might look something like:
typedef struct dynamic_array_struct
{
int* data;
size_t capacity; /* total capacity */
size_t size; /* number of elements in vector */
} vector;
Then they would have various API function calls which operate on the vector
:
int vector_init(vector* v, size_t init_capacity)
{
v->data = malloc(init_capacity * sizeof(int));
if (!v->data) return -1;
v->size = 0;
v->capacity = init_capacity;
return 0; /* success */
}
Then of course, you need functions for push_back
, insert
, resize
, etc, which would call realloc
if size
exceeds capacity
.
vector_resize(vector* v, size_t new_size);
vector_push_back(vector* v, int element);
Usually, when a reallocation is needed, capacity
is doubled to avoid reallocating all the time. This is usually the same strategy employed internally by std::vector
, except typically std::vector
won't call realloc
because of C++ object construction/destruction. Rather, std::vector
might allocate a new buffer, and then copy construct/move construct the objects (using placement new
) into the new buffer.
An actual vector implementation in C might use void*
pointers as elements rather than int
, so the code is more generic. Anyway, this sort of thing is implemented in a lot of C projects. See http://codingrecipes.com/implementation-of-a-vector-data-structure-in-c for an example vector implementation in C.