I made a somewhat simple vector implementation in C. Right now I only have around 5 features (making a vector, adding elements, removing elements, changing elements, deleting the vector). I'm proud of it but I know it needs a lot of work so I came here to see what you think I could improve. There are 3 files, main.c
(tests), Arr.h
(function definitions, macros and structs) and Arr.c
. Here is the code for all 3 files:
Arr.h
//Arr.h #ifndef ARR_H #define ARR_H #define growCapacity(capacity) \ (capacity < 8) ? 8 : capacity*2 typedef struct{ int count; int capacity; int* Array; }Vect; void initVect(Vect* vect); Vect* constructVect(int array[], int count, int capacity); void addVect(Vect* vect, int data); void changeVect(int data, int index, Vect* vect); void removeVect(Vect* vect, int count); #endif
Arr.c
//Arr.c #include "Arr.h" #include <stdio.h> #include <stdlib.h> #include <string.h> void* reallocate(Vect* vect, size_t oldSize, size_t newSize){ if(newSize == 0){ free(vect->Array); return NULL; } void* result = realloc(vect->Array, newSize); if(result == NULL){ printf("There is not enough memory to reallocate array. Exiting"); exit(1); } return result; } void initVect(Vect* vect){ vect->count = 0; vect->capacity = 0; vect->Array = NULL; } Vect* constructVect(int array[], int count, int capacity){ //Find a way to get the count and size of an array by using a function rather than having it as a parameter Vect* vect = (Vect*)malloc(sizeof(Vect)); vect->capacity = capacity; vect->count = count; vect->Array = (int*)malloc(capacity); memcpy(vect->Array, array, capacity); return vect; } void addVect(Vect* vect, int data){ if(vect->capacity < (vect->count*4)+1){ int oldCapacity = vect->capacity; vect->capacity = growCapacity(oldCapacity); vect->Array = (int*)reallocate(vect, oldCapacity, vect->capacity); } vect->Array[vect->count] = data; vect->count++; } void changeVect(int data, int index, Vect* vect){ if(index > vect->count){ fprintf(stderr, "ERR: element does not exist..."); exit(-1); } vect->Array[index] = data; } void removeVect(Vect* vect, int count){ vect->count -= count; //Removes amount of vectors specified in count, the data is not fully deleted but it is no longer accessible }
main.c
#include <stdio.h> #include <stdlib.h> #include "Arr.h" int main(){ int array[] = {1, 2, 3, 4 , 5}; int count = sizeof(array)/sizeof(array[0]); int capacity = sizeof(array); Vect* vecto = constructVect(array, count, capacity); for(int i = 0; i<vecto->count; i++){ //Write elements printf("Count: %d Capacity: %d Element: %d\n", vecto->count, vecto->capacity, vecto->Array[i]); } printf("\n\n Updated after adding 2 elements\n\n"); addVect(vecto, 7); addVect(vecto, 9); for(int i = 0; i<vecto->count; i++){ //Write elements printf("Count: %d Capacity: %d Element: %d\n", vecto->count, vecto->capacity, vecto->Array[i]); } printf("\n\n Changed element\n\n"); changeVect(54, 0, vecto); for(int i = 0; i<vecto->count; i++){ //Write elements printf("Count: %d Capacity: %d Element: %d\n", vecto->count, vecto->capacity, vecto->Array[i]); } printf("\n\n Update after removing 2 elements\n\n"); removeVect(vecto, 2); for(int i = 0; i<vecto->count; i++){ printf("Count: %d Capacity: %d Element: %d\n", vecto->count, vecto->capacity, vecto->Array[i]); } return 0; }