C99 allows to define a flexible array member as the last member of a struct
, namely an array of undetermined length.
P99_DECLARE_STRUCT(package_head); struct package_head { char name[20]; size_t len; uint64_t data[]; };
Such a struct
can then allocated on the heap with a suitable size such that the field data has as much elements as fit in the allocated space from the start of data onward. Usually one would allocate such struct
with
package_head *a = malloc(sizeof(package_head) + 10 * sizeof(uint64_t)); package_head *b = malloc(sizeof(*b) + 12 * sizeof(b->data[0]));
This has several disadvantages. First the syntax is clumsy, we have to use a relatively complicated expression that uses two elements of the specification of a
or b
.
Then this is wasting space. Due to packing of the struct
the offset of data “inside” the struct
may be less than sizeof(package_head)
. In most cases the real size of the object that we want to build is
offsetof(package_head, data) + N * sizeof(uint64_t)
so we are wasting
sizeof(package_head) - offsetof(package_head, data)
bytes.
The above formula for the exact size is only valid for larger values of N
. We must also make sure that we allocate at least sizeof(package_head)
bytes. So the complete formula for looks something like
P99_MAXOF(sizeof(T), offsetof(T, F) + P99_SIZEOF(T, F[0]) * N)
which is probably not something that you want to write on a daily base. A particularity in that expression is P99_SIZEOF(T, F[0])
which stands for the size of the element F[0]
inside the struct
type T
. C doesn’t have the possibility as C++ to refer to a field in a type with something like T::F
.
Something similar can be obtained in C99 with the magic formula sizeof((T){ 0 }.F[0])
: define a compound literal (T){ 0 }
of type T
and take its field F
. The sizeof
operator actually ensures that this compound literal is never allocated, only the field F
is taken for its type and size. This magic works in function scope (the compound literal would be of storage class auto
) and in file scope (it would be static
).
P99 provides several interfaces to allocate struct
with flexible members: P99_FCALLOC
, P99_FMALLOC
and P99_FREALLOC
.
I don’t see how using
offsetof()
buys you anything. Sincestruct
s with flexible array members cannot be stored in arrays, the only padding between the penultimatestruct
member and the FAM will be that which is needed for alignment of the array.Have you actually found a system in which
sizeof(package_head) != offsetof(package_head, data)
?I took another look at the Standard, and I think you’re right.
§6.7.2.1 ¶16 says,
Consider a system with 64-bit
long
aligned to 8 bytes, 32-bitsize_t
aligned to 4 bytes, andchar
having no alignment requirements. The structurewould (since it must be storable in arrays) have a layout something like
[tttt_tttt_llll_pppp]
with a size of 16 bytes. The structure
would then be almost equivalent to
with
struct bar
having the layout[tttt_tttt_llll|pppp]
andstruct bas
the layout[tttt_tttt_llll_dddd]
.If the Standard, instead of saying, “it may have more trailing padding than the omission would imply”, would have said, “it may have different trailing padding than the omission would imply”,
struct bar
could have had the layout[tttt_tttt_llll](d…)
—i.e., withsizeof(struct bar) == offsetof(struct bar, data)
. But such is not the case, as ¶17–19 make clear.Yes exactly. I came up with the following
which gives me
with gcc on my 64bit ubuntu system.
Note that the C99 Rationale, §6.7.2.1 (near line #20) is somewhat misleading and says, “
sizeof
applied to the structure ignores the array but counts any padding before it. This makes themalloc
call as simple as possible.”