What is the memory layout of structs, tuples and tuple structs?

做~自己de王妃 提交于 2021-01-27 05:54:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!