Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use "pictures" to explain degrees of freedom in struct and union layout #185

Closed
RalfJung opened this issue Aug 1, 2019 · 3 comments
Closed
Labels
A-layout Topic: Related to data structure layout (`#[repr]`) C-editorial Category: Tracking editorial improvements to the document

Comments

@RalfJung
Copy link
Member

RalfJung commented Aug 1, 2019

In #160 (comment) I suggested to use "pictures" to explain what happens when a struct/union is laid out (at least in terms of size and field offsets) and how we call the movable pieces there.

For structs:

[  <--> [field 1] <--> [field 2] <--> [field 3] <--> ]

This shows we can have leading, inner, and trailing "padding".

For unions:

[ <--> [field 1] <----> ]
[ <----> [field 2] <--> ]
[ <--> [ field 3 ] <--> ]

This shows that for each field/variant, we can have (separate) leading and trailing padding.

I think it would be nice to incorporate this into the document. But for this we basically need consensus that how big the padding is and (only for structs) in which order the fields come is the only freedom the compiler has to lay out these types. I am pretty sure that's true, e.g. we cannot split a field because you can take a reference to a field and that has to be using the layout of the type of that field -- but better safe than sorry. ;)

@Lokathor
Copy link
Contributor

Lokathor commented Aug 1, 2019

We can call it ASCII Art and be proud of it <3

@RalfJung
Copy link
Member Author

RalfJung commented Aug 1, 2019

I submitted a PR doing this for structs: #186

For unions I think this should happen in #161. It already contains all the rationale.

@RalfJung RalfJung added C-editorial Category: Tracking editorial improvements to the document A-layout Topic: Related to data structure layout (`#[repr]`) labels Aug 14, 2019
@RalfJung
Copy link
Member Author

This has landed both for structs and unions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-layout Topic: Related to data structure layout (`#[repr]`) C-editorial Category: Tracking editorial improvements to the document
Projects
None yet
Development

No branches or pull requests

2 participants