|
|
|
|
|
by kkdwivedi
2250 days ago
|
|
Another option is a struct with a FAM at the end. typedef struct {
size_t len;
uint8_t data[];
} ByteBuf;
Then, allocation becomes ByteBuf *b = malloc(sizeof(*b) + sizeof(uint8_t) * array_size);
b->len = array_size;
and data is no longer a pointer. |
|
Thing is, you rarely want to share just a buffer anyway. You probably have additional state, locks, etc. So what I do is embed my ByteBuf directly into another structure, which then owns it completely:
So we end up with the same amount of pointers (1), but with some unique advantages.