A simple implementation of a dynamic array inspired by Go slices.
This project demonstrates how slices can be built in C using:
- raw memory allocation (
malloc,realloc) - pointer arithmetic
- generic storage (
void*) - manual memory management
It is not production-ready, but meant for learning and experimentation.
- dynamic resizing (
append) - slicing (
sub) - element access (
get,set) - copying (
copy) - manual memory control (
delete) - generic type support via
elem_size - basic error handling / logging system
Each slice is represented as:
typedef struct {
void *ptr;
size_t len;
size_t cap;
size_t elem_size;
bool is_owner;
} Slice;#define make(type, len, cap)slice_make(sizeof(type), len, cap)— Create a slice.#define delete(s)slice_free(&(s))— Free a slice.
#define len(s)slice_len(s)— Get slice length.#define cap(s)slice_cap(s)— Get slice capacity.
#define get(s, index)slice_get(s, index)— Get element by index.#define set(s, index, elem)slice_set(s, index, &(elem))— Set element value.#define append(s, elem)slice_append(s, &(elem))— Append element to end.
#define copy(dst, src)slice_copy(dst, src)— Copy slice data.#define sub(s, start, end)slice_sub(s, start, end)— Create a sub-slice.
Run the example program using the following command:
make example-run