3 ms·
I doubt you can count on all items being allocated at addresses that are multiples if their size. It's not optimal but you can always use libc::posix_memalign(
by wyldfire 10y ago
I doubt you can count on all items being allocated at addresses that are multiples if their size.
It's not optimal but you can always use libc::posix_memalign()
- alkonaut 10y agoThe final element of the struct is a zero length array of elements of size N bytes. So that element isn't padding, it has size 0! It's pure hinting. I'm not sure why or how this works under the hood I'm afraid. I used it successfully to call a library with pretty strict alignment requirements (Intel Embree).
- Thiez 10y agoThe zero length array still must be aligned correctly. You can create a pointer to it (e.g. by taking &something._alignment[..] to create a slice) and pointers must have correct alignment, so it follows that a zero length array has the same alignment requirements as a longer array. So padding must be inserted in your struct so that the address of the zero-length array is correctly aligned.
- alkonaut 10y agoHmm, I don't understand (sorry). The pointer to the internal array must be aligned. But how does the element size play into the padding. If we take these two examples? [repr(C)] struct StructA { pub foo: f32, _alignment: [SixteenBytes, 0] } [repr(C)] struct StructB { pub foo: f32, _alignment: [ThirtytwoBytes, 0] } Are both just padded with 16-4 and 32-4 bytes respectively, so they are equivalent to making a padding like this in the first case? [repr(C)] struct StructAPadded { pub foo: f32, _padding : TwelveBytes; }