Skip to content

A simple vector library for my personal projects

Notifications You must be signed in to change notification settings

Lunaaaalj/cvector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cvector - Dynamic Vector Library for C

A lightweight, simple dynamic vector (dynamic array) implementation for C, similar to C++ vectors or Python lists.

Features

  • 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

Installation

Building the Library

cd src
mkdir build
cd build
cmake ..
make

This will create a static library libcvector.a in the build/lib directory.

Installing

sudo make install

This installs the library to /usr/local/lib and the header to /usr/local/include.

Usage

Integer Vector Example

#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;
}

Generic (Void) Vector Example

#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;
}

API Reference

Integer Vector Functions

int init_vector(vector *vec, size_t initial_capacity)

Initializes an integer vector with the specified initial capacity.

Parameters:

  • vec: Pointer to the vector struct to initialize
  • initial_capacity: Initial number of elements to allocate space for

Returns:

  • 0 on success
  • 1 if vec is NULL
  • 2 on memory allocation failure
  • 3 on overflow error

int append(vector *vec, int element)

Appends an integer to the vector. Automatically resizes if needed.

Parameters:

  • vec: Pointer to the vector
  • element: Integer value to append

Returns:

  • 0 on success
  • 1 if vec or vec->data is NULL
  • 2 on memory allocation failure
  • 3 on overflow error

void free_vector(vector *vec)

Frees the memory allocated for the vector's data.

Parameters:

  • vec: Pointer to the vector to free

Generic Vector Functions

int init_void_vector(void_vec *vec, size_t initial_capacity, size_t type_size)

Initializes a generic vector for storing any data type.

Parameters:

  • vec: Pointer to the void_vec struct to initialize
  • initial_capacity: Initial number of elements to allocate space for
  • type_size: Size of each element (use sizeof(YourType))

Returns:

  • 0 on success
  • 1 if vec is NULL or type_size is 0
  • 2 on memory allocation failure
  • 3 on overflow error

int void_append(void_vec *vec, const void *element)

Appends an element to the generic vector. Automatically resizes if needed.

Parameters:

  • vec: Pointer to the vector
  • element: Pointer to the element to append

Returns:

  • 0 on success
  • 1 if vec, element, or vec->data is NULL
  • 2 on memory allocation failure
  • 3 on overflow error

void *void_get(const void_vec *vec, const size_t index)

Retrieves a pointer to the element at the specified index.

Parameters:

  • vec: Pointer to the vector
  • index: Index of the element to retrieve

Returns:

  • Pointer to the element on success
  • NULL if vec is NULL, vec->data is NULL, or index is out of bounds

int void_set(void_vec *vec, const void *element, const size_t index)

Sets the value of an element at the specified index.

Parameters:

  • vec: Pointer to the vector
  • element: Pointer to the new value
  • index: Index to set

Returns:

  • 0 on success
  • 1 if parameters are invalid or index is out of bounds

void free_void_vector(void_vec *vec)

Frees the memory allocated for the generic vector's data.

Parameters:

  • vec: Pointer to the vector to free

Error Handling

All functions that can fail set errno to indicate the error type:

  • EINVAL: Invalid parameter (NULL pointer, invalid index)
  • ENOMEM: Memory allocation failure
  • EOVERFLOW: Capacity would overflow

Check return values and errno for proper error handling.

Limitations

  • Integer vectors are limited to storing int type only
  • No built-in functions for removal, insertion, or searching
  • Manual memory management required (must call free functions)
  • Not thread-safe

License

This is a basic educational/personal use library. Check with the repository owner for licensing details.

Contributing

This library is designed for basic personal use. For production use, consider more comprehensive libraries like:

About

A simple vector library for my personal projects

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors