Implementing a Vector in C: A Practical Guide
While C does not natively support dynamic arrays like higher-level languages such as Python, it is indeed possible to implement a vector-like structure using a combination of structures and dynamic memory allocation. This article will guide you through creating a simple vector implementation in C, providing a detailed explanation and example code.
Introduction to Vector Implementation in C
Dynamic arrays in C are typically implemented using a combination of a structure to hold the array pointer and a size counter. While C lacks built-in support for vectors, it offers means to dynamically allocate memory and resize arrays as needed. This article will demonstrate how to create a basic vector implementation in C.
Structure Definition
A vector structure in C consists of a pointer to the element array, the current size of the vector, and the capacity of the vector (i.e., the maximum number of elements that can be stored).
typedef struct {
int* data // Pointer to the array of elements
size_t size // Current number of elements
size_t capacity // Maximum number of elements that can be stored
} Vector;
Creating a Vector
The create_vector function initializes a new vector with a specified initial capacity.
Code Example
Vector create_vector(size_t initial_capacity) {
Vector vec malloc(sizeof(Vector));
vec-data malloc(initial_capacity * sizeof(int));
vec-size 0;
vec-capacity initial_capacity;
return vec;
}
Adding Elements to the Vector
The vector_push_back function adds elements to the vector. If the current size reaches the capacity, it doubles the capacity and reallocates memory.
Code Example
void vector_push_back(Vector vec, int value) {
if (vec-size vec-capacity) {
// Resize the vector if necessary
vec-capacity vec-capacity * 2;
vec-data realloc(vec-data, vec-capacity * sizeof(int));
}
vec-data[vec-size] value;
vec-size ;
}
Accessing Elements in the Vector
The vector_get function retrieves elements from the vector, checking for out-of-bounds access.
Code Example
int vector_get(Vector vec, size_t index) {
if (index vec-size) {
fprintf(stderr, "Error: Out of bounds access
");
exit(EXIT_FAILURE);
}
return vec-data[index];
}
Memory Management
The free_vector function cleans up the allocated memory for the vector.
Code Example
void free_vector(Vector vec) {
free(vec-data);
free(vec);
}
Usage
You can compile and run the following code using a C compiler. It demonstrates creating a vector, adding elements, accessing them, and cleaning up resources.
Code Example
#include stdio.h
#include stdlib.h
typedef struct {
int* data // Pointer to the array of elements
size_t size // Current number of elements
size_t capacity // Maximum number of elements that can be stored
} Vector;
Vector create_vector(size_t initial_capacity) {
Vector vec malloc(sizeof(Vector));
vec-data malloc(initial_capacity * sizeof(int));
vec-size 0;
vec-capacity initial_capacity;
return vec;
}
void vector_push_back(Vector vec, int value) {
if (vec-size vec-capacity) {
vec-capacity vec-capacity * 2;
vec-data realloc(vec-data, vec-capacity * sizeof(int));
}
vec-data[vec-size] value;
vec-size ;
}
int vector_get(Vector vec, size_t index) {
if (index vec-size) {
fprintf(stderr, "Error: Out of bounds access
");
exit(EXIT_FAILURE);
}
return vec-data[index];
}
void free_vector(Vector vec) {
free(vec-data);
free(vec);
}
int main() {
Vector vec create_vector(4); // Create a vector with an initial capacity of 4
// Add elements to the vector
for (int i 0; i 10; i ) {
vector_push_back(vec, i);
}
// Print elements in the vector
for (size_t i 0; i vec-size; i ) {
printf("%d ", vector_get(vec, i));
}
printf("
");
// Free the vector
free_vector(vec);
return 0;
}
In this simple implementation, we have demonstrated the basic functionality of a vector in C. This example can be extended with additional functionality like removing elements, inserting at specific positions, and more utility functions.