M
Michael B Allen
Let's say I have a stucture with a flexible array at the end:
struct foo {
short s;
char bits[1];
};
I would like to embed this in other structures like:
#define TAILBITS 500
struct bar {
int i;
struct foo f;
char tail[TAILSIZE(500)];
};
My question is, can someone define a macro TAILSIZE that evaluates to
an optimal value for holding the specified number of bits in foo->bits?
Like a good poster I have tried this beforehand and came up with the
following that takes advantage of any padding in the structure:
#define PAD (sizeof(struct foo) - offsetof(struct foo, bits))
#define BYTES4BITS(nbits) (((nbits) + 1) / 8)
#define TAILSIZE(nbits) ((PAD + 1) > BYTES4BITS(nbits) ? 1 : BYTES4BITS(nbits) - PAD)
At first glance this appears to work but is it legit?
Thanks,
Mike
struct foo {
short s;
char bits[1];
};
I would like to embed this in other structures like:
#define TAILBITS 500
struct bar {
int i;
struct foo f;
char tail[TAILSIZE(500)];
};
My question is, can someone define a macro TAILSIZE that evaluates to
an optimal value for holding the specified number of bits in foo->bits?
Like a good poster I have tried this beforehand and came up with the
following that takes advantage of any padding in the structure:
#define PAD (sizeof(struct foo) - offsetof(struct foo, bits))
#define BYTES4BITS(nbits) (((nbits) + 1) / 8)
#define TAILSIZE(nbits) ((PAD + 1) > BYTES4BITS(nbits) ? 1 : BYTES4BITS(nbits) - PAD)
At first glance this appears to work but is it legit?
Thanks,
Mike