Skip to content

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

Closed
@RalfJung

Description

@RalfJung

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. ;)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-layoutTopic: Related to data structure layout (`#[repr]`)C-editorialCategory: Tracking editorial improvements to the document

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions