#ZST alignment

6 messages · Page 1 of 1 (latest)

worthy knot
#

Just reading nomicon a bit and reading about handling ZSTs it seemed to imply that although ptr::read is a no-op for ZST the pointer should still be aligned, and that if just blindly incrementing the pointer by 1 each time it might not be aligned.

However I always assumed ZSTs would be guaranteed to have an alignment of 1 (minimal alignment) so incrementing pointer address each time by 1 wouldn't be able to misalign it.

Is it correct that we can't guarantee ZSTs have an alignment of 1 then?, or is the author being overly cautious there?.

mellow saddle
jovial aspenBOT
#
     Running `target/debug/playground`

8
mellow saddle
#

"normal" ZSTs have align 1

#

(), struct Foo;, struct Bar(Foo, (), Foo);

#

however you can set the align with #[align(N)], and for example arrays have align of the element