Stream: t-lang/wg-unsafe-code-guidelines

Topic: null pointer address alignment

gnzlbg (Nov 22 2018 at 14:21, on Zulip):

Question: is the address of null pointer (ptr::null()/ptr::null_mut()) always a multiple of the alignment of T ?

gnzlbg (Nov 22 2018 at 14:22, on Zulip):

For 0x0 and 0x1 this is true, but it wouldn't be true for 0xfffff.

rkruppe (Nov 22 2018 at 14:23, on Zulip):

it is if that address is 0; if not then clearly it cannot be for arbitrary T unless we restrict the address in a ridiculous manner like requiring it's either 0 or the middle of the address space

rkruppe (Nov 22 2018 at 14:23, on Zulip):

0x1 is not aligned for any T except for with align 1

rkruppe (Nov 22 2018 at 14:25, on Zulip):

why does it matter anyway? usually alignment matters because you want it to be a reference (but references aren't nullable) or because you want to stash something in the lower order bits (which you can do regardless of the address of the null pointer if you don't need to tag the null pointer, which typically you don't)

Last update: Jul 14 2020 at 13:25UTC