public
Last active

Simple vector-implementation in C

  • Download Gist
vector.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
#include <stdio.h>
#include <stdlib.h>
 
#include "vector.h"
 
void vector_init(vector *v)
{
v->data = NULL;
v->size = 0;
v->count = 0;
}
 
int vector_count(vector *v)
{
return v->count;
}
 
void vector_add(vector *v, void *e)
{
if (v->size == 0) {
v->size = 10;
v->data = malloc(sizeof(void*) * v->size);
memset(v->data, '\0', sizeof(void) * v->size);
}
 
// condition to increase v->data:
// last slot exhausted
if (v->size == v->count) {
v->size *= 2;
v->data = realloc(v->data, sizeof(void*) * v->size);
}
 
v->data[v->count] = e;
v->count++;
}
 
void vector_set(vector *v, int index, void *e)
{
if (index >= v->count) {
return;
}
 
v->data[index] = e;
}
 
void *vector_get(vector *v, int index)
{
if (index >= v->count) {
return;
}
 
return v->data[index];
}
 
void vector_delete(vector *v, int index)
{
if (index >= v->count) {
return;
}
 
v->data[index] = NULL;
 
int i, j;
void **newarr = (void**)malloc(sizeof(void*) * v->count * 2);
for (i = 0, j = 0; i < v->count; i++) {
if (v->data[i] != NULL) {
newarr[j] = v->data[i];
j++;
}
}
 
free(v->data);
 
v->data = newarr;
v->count--;
}
 
void vector_free(vector *v)
{
free(v->data);
}
 
int main(void)
{
vector v;
vector_init(&v);
 
vector_add(&v, "emil");
vector_add(&v, "hannes");
vector_add(&v, "lydia");
vector_add(&v, "olle");
vector_add(&v, "erik");
 
int i;
printf("first round:\n");
for (i = 0; i < vector_count(&v); i++) {
printf("%s\n", vector_get(&v, i));
}
 
vector_delete(&v, 1);
vector_delete(&v, 3);
 
printf("second round:\n");
for (i = 0; i < vector_count(&v); i++) {
printf("%s\n", vector_get(&v, i));
}
 
vector_free(&v);
 
return 0;
}
vector.h
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#ifndef VECTOR_H__
#define VECTOR_H__
 
typedef struct vector_ {
void** data;
int size;
int count;
} vector;
 
void vector_init(vector*);
int vector_count(vector*);
void vector_add(vector*, void*);
void vector_set(vector*, int, void*);
void *vector_get(vector*, int);
void vector_delete(vector*, int);
void vector_free(vector*);
 
#endif

Do you have a license in mind for this gist? Can we assume it's an MIT license?

There is a bug in vector_delete. The v->size should be updated after new array is allocated (because it has different size than the old one). This leads to memory leaks and/or segfaults.

for vector_add, is it should be "memset(v->data, '\0', sizeof(void *) * v->size)", not sizeof(void)

vector_delete doesn't have to reallocate a new memory block. Especially if you aren't changing the size of the array and are only allowing one item to be deleted at a time, then just start at the index that you are going to delete and shift everything down one, setting the last index to NULL.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.