I create(nothing serious here) a .c file and a .h file and I want to know if I set them up correctly. I want to expose the struct and its elements(struct slice) and the function createSlice. I want everything else to be not be available.
slice.h
Code:
#include <stdlib.h>
#ifndef SLICE_HH
#define SLICE_HH
struct slice
{
size_t pos;
size_t length;
void (*free_slice)(const struct slice *);
void(*append)(struct slice *, unsigned int);
unsigned int (*get)(const struct slice *, size_t);
void (*display)(const struct slice*);
unsigned int *arr;
};
struct slice *createSlice(const size_t length);
#endif
slice.c
Code:
#include <stdio.h>
#include <stdlib.h>
struct slice
{
size_t pos;
size_t length;
void (*free_slice)(const struct slice *);
void(*append)(struct slice *, unsigned int);
unsigned int (*get)(const struct slice *, size_t);
void (*display)(const struct slice*);
unsigned int *arr;
};
static void check_allocation(const void *ptr)
{
if (!ptr)
{
fputs("Allocation failed!", stderr);
exit(1);
}
}
static void display(const struct slice * s) {
size_t i = 0;
fprintf(stdout, "Pos: %ld, Length: %ld\n", s->pos, s->length);
fputs("[ ", stdout);
for (; i < s->length; i++) {
fprintf(stdout, "%d ", s->arr[i]);
}
fputs("]\n", stdout);
}
static unsigned int get(const struct slice * s, size_t pos) {
if (pos <= s->pos) {
return s->arr[pos];
}else {
exit(1);
}
}
static void append(struct slice *s, unsigned int value)
{
if (s->pos < s->length)
{
s->arr[s->pos++] = value;
return;
}
s->arr = realloc(s->arr, (s->length * 2) * sizeof(unsigned int));
check_allocation(s->arr);
s->length = s->length * 2;
s->arr[s->pos++] = value;
}
static void free_slice(const struct slice *s)
{
free((void *)s->arr);
free((void *)s);
}
struct slice *createSlice(const size_t length)
{
struct slice *s = malloc(sizeof(*s));
check_allocation(s);
s->arr = calloc(sizeof(unsigned int), length);
check_allocation(s->arr);
s->pos = 0;
s->length = length;
s->free_slice = free_slice;
s->append = append;
s->get = get;
s->display = display;
return s;
}
Am I using the static qualifier correctly here?