I would like to implement this C code which uses a flexible array member (sometimes called the struct hack) in Rust:
struct test {
int key;
int dataSize;
int data[];
};
struct test* t = malloc(sizeof(struct test) + sizeOfData)
The empty array at the end of structure allows you to allocate your meta fields and data all at once. Unfortunately, I can't figure out how to do such thing in Rust.
unsafe
. But I'm not familiar enough with how it works in C. What if the alignment of thedata
array is bigger than the size of the header -- are padding bytes counted in thesizeof
, or do we just rely onmalloc
allocating more than necessary to satisfy the maximum possible alignment? – Mirisola