A lightweight, simple dynamic vector (dynamic array) implementation for C, similar to C++ vectors or Python lists.
- Integer Vectors: Optimized storage for integer arrays
- Generic Vectors: Type-safe storage for any data type
- Automatic Resizing: Grows automatically when capacity is reached
- Memory Safe: Proper error handling and bounds checking
- Simple API: Easy to use with minimal boilerplate
cd src
mkdir build
cd build
cmake ..
makeThis will create a static library libcvector.a in the build/lib directory.
sudo make installThis installs the library to /usr/local/lib and the header to /usr/local/include.
#include "vector.h"
#include <stdio.h>
int main() {
vector vec;
// Initialize vector with initial capacity of 10
if (init_vector(&vec, 10) != 0) {
fprintf(stderr, "Failed to initialize vector\n");
return 1;
}
// Append elements
append(&vec, 42);
append(&vec, 100);
append(&vec, 200);
// Access elements
printf("Element 0: %d\n", vec.data[0]);
printf("Vector size: %zu\n", vec.size);
printf("Vector capacity: %zu\n", vec.capacity);
// Clean up
free_vector(&vec);
return 0;
}#include "vector.h"
#include <stdio.h>
typedef struct {
int id;
char name[50];
} Person;
int main() {
void_vec vec;
// Initialize vector for Person structs
if (init_void_vector(&vec, 10, sizeof(Person)) != 0) {
fprintf(stderr, "Failed to initialize vector\n");
return 1;
}
// Create and append elements
Person p1 = {1, "Alice"};
Person p2 = {2, "Bob"};
void_append(&vec, &p1);
void_append(&vec, &p2);
// Access elements
Person *retrieved = (Person *)void_get(&vec, 0);
if (retrieved) {
printf("Person 0: ID=%d, Name=%s\n", retrieved->id, retrieved->name);
}
// Modify elements
Person p3 = {1, "Alice Updated"};
void_set(&vec, &p3, 0);
// Clean up
free_void_vector(&vec);
return 0;
}Initializes an integer vector with the specified initial capacity.
Parameters:
vec: Pointer to the vector struct to initializeinitial_capacity: Initial number of elements to allocate space for
Returns:
0on success1ifvecis NULL2on memory allocation failure3on overflow error
Appends an integer to the vector. Automatically resizes if needed.
Parameters:
vec: Pointer to the vectorelement: Integer value to append
Returns:
0on success1ifvecorvec->datais NULL2on memory allocation failure3on overflow error
Frees the memory allocated for the vector's data.
Parameters:
vec: Pointer to the vector to free
Initializes a generic vector for storing any data type.
Parameters:
vec: Pointer to the void_vec struct to initializeinitial_capacity: Initial number of elements to allocate space fortype_size: Size of each element (usesizeof(YourType))
Returns:
0on success1ifvecis NULL ortype_sizeis 02on memory allocation failure3on overflow error
Appends an element to the generic vector. Automatically resizes if needed.
Parameters:
vec: Pointer to the vectorelement: Pointer to the element to append
Returns:
0on success1ifvec,element, orvec->datais NULL2on memory allocation failure3on overflow error
Retrieves a pointer to the element at the specified index.
Parameters:
vec: Pointer to the vectorindex: Index of the element to retrieve
Returns:
- Pointer to the element on success
NULLifvecis NULL,vec->datais NULL, or index is out of bounds
Sets the value of an element at the specified index.
Parameters:
vec: Pointer to the vectorelement: Pointer to the new valueindex: Index to set
Returns:
0on success1if parameters are invalid or index is out of bounds
Frees the memory allocated for the generic vector's data.
Parameters:
vec: Pointer to the vector to free
All functions that can fail set errno to indicate the error type:
EINVAL: Invalid parameter (NULL pointer, invalid index)ENOMEM: Memory allocation failureEOVERFLOW: Capacity would overflow
Check return values and errno for proper error handling.
- Integer vectors are limited to storing
inttype only - No built-in functions for removal, insertion, or searching
- Manual memory management required (must call free functions)
- Not thread-safe
This is a basic educational/personal use library. Check with the repository owner for licensing details.
This library is designed for basic personal use. For production use, consider more comprehensive libraries like: