It may be that at times, for performance reasons, I may decide to define a struct such as
typedef struct {
size_t capacity;
size_t size;
int offset;
void* data[];
} ring_buffer;
inlining data
in the struct itself. I'm currently then defining the creation function as
ring_buffer* ring_buffer_create(size_t capacity) {
int struct_size =
sizeof(int) + 2 * sizeof(size_t) +
capacity * sizeof(void*);
ring_buffer* rb = (ring_buffer*)malloc(struct_size);
rb->capacity = capacity;
rb->offset = 0;
rb->size = 0;
return rb;
}
which (if I didn't miscalculate anything) will work fine as long as the C compiler doesn't do some weird field padding / alignments.
How do people in general deal with this situation (other than defining data
to be a pointer, of course)?
Thanks