问题
It is clear from the reference manual that the memory layout of structs is unspecified (when the repr
attribute is not used). This rule gives the compiler the possibility to pack structures tighter by reordering the fields.
What about the memory layout of tuples and tuple structs? How is it (un)specified and why?
回答1:
The memory layout of tuples and tuple structs is undefined, just like the layout of normal structs, with one exception:
The exception to this is the unit tuple (
()
) which is guaranteed as a zero-sized type to have a size of 0 and an alignment of 1.
The compiler can make the same optimizations in tuples and tuple structs that it can in structs, it just has to re-order matches of them as well.
来源:https://stackoverflow.com/questions/25650128/what-is-the-memory-layout-of-structs-tuples-and-tuple-structs