Skip to content

a vector I made to learn about memory and pointers in C.

Notifications You must be signed in to change notification settings

GiovanyH/c-vector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

C-Vector

Code:

#define new_vector(TYPE, NAME) \
    TYPE *NAME = (TYPE*)((int*)malloc(2 * sizeof(int) + sizeof(TYPE) * 8) + 2); \
    ((int *)NAME)[-2] = 0; ((int *)NAME)[-1] = 8;

#define vec_get_hdr(SB) ((int*)(SB) - 2)
#define vec_push(SB, ELEM) vec_expand((void**)&(SB), sizeof(ELEM), vec_get_hdr(SB)), (SB)[vec_get_hdr(SB)[0]++] = (ELEM)
#define vec_pop(SB, TYPE) vec_shrink((void**)&(SB), sizeof(TYPE), vec_get_hdr(SB))

static inline void vec_expand(void **sb, size_t isz, int *h) {
    if (h[0] >= h[1]) *sb = (void*)((int*)realloc(h, 2 * sizeof(int) + (h[1] *= 2) * isz) + 2);
}

static inline void vec_shrink(void **sb, size_t isz, int *h) {
    if (--h[0] < h[1] - 8) *sb = (void*)((int*)realloc(h, 2 * sizeof(int) + (h[1] -= 8) * isz) + 2);
}

Have fun!

About

a vector I made to learn about memory and pointers in C.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages