What is an misaligned pointer

The reference:

For instance, if ptr has type *const S where S has an alignment of 8, then ptr must be 8-aligned or else (*ptr).f is "based on an misaligned pointer"

What the document means; S is 8-aligned, then ptr has to 8-aligned? What if S is 16-aligned? Then
assert!((&ptr as const* _ as isize) & 0xf ) == 0) always be true?

Having an alignment greater than that required is acceptable, as it necessarily means that all smaller alignments are true as well for the reason you stated.

No. I think the document is not correct.
align_of(ptr) should be 8 at most arch. Maybe even align_of(fatptr) is 8 too.

It is unacceptable that align_of(*const T) == align_of(T)

k-alignment generally refers to a memory address a where a ≡ 0 (mod k).

In the case of a pointer, there are two relevant memory addresses:

  1. The address of the pointee, i.e. the value stored within the pointer
  2. The memory location where the pointer itself is stored

Unfortunately, this leads to an ambiguity in a lot of documentation. Either of these can be meant when talking about the alignment of a pointer, and which one is relevant has to be inferred from context.


This statement is true, referring to the location of the pointee S:

As is this one on currently-supported platforms, now referring to the address where ptr is stored:

But this one is not generally true, because it's mixing the two senses of a pointer's alignment:

1 Like

eh, the assert!((&S as *const S as isize) & 0xf ) == 0) are not proper codes

For instance, if ptr has type *const S where S has an alignment of 8, then ptr must be 8-aligned or else (*ptr).f is "based on an misaligned pointer"

talks about ptr must be 8-aligned because S is 8-aligned, while my assert!((&S as *const S as isize) & 0xf ) == 0) talks about the value of ptr ...

Have modified the code in main posting.

Your updated code is still referring to the value of the pointer / the location of an S instance. If you want to talk about the location of the pointer instead, you'll need to do something with &ptr or similar.

Alignment of S: 8, Alignment of *const S: 8
Alignment of T: 16, Alignment of *const T: 8

I don't get your issue as

align_of(*const T) == align_of(T)

Is clearly not true.

faint... update again

Yes, so the document is misleading, that is what I want to say.

The formulation is potentially confusing, I’ll admit, however note that the document did never speak of the alignment of *const S. It speaks of the alignment of ptr. There is a distinction between “TYPE has alignment of N” and “POINTER_VALUE_EXPRESSION as N-aligned” which differs both in the specific wording “has alignment” vs “is aligned” and the fact that we’re talking about different types of things, types vs. pointer values.

Of course it all could probably still be made more clear if we just said “POINTER_VALUE_EXPRESSION’s address is N-aligned”:

For instance, if ptr has type *const S where S has an alignment of 8, then ptr’s address must be 8-aligned or else (*ptr).f is "based on an misaligned pointer"

On the other hand, that wording would arguably be more inconsistent with the usage of “misaligned pointer” in other places. “the pointer is misaligned” and “POINTER_VALUE_EXPRESSION as N-aligned” seems very consistent.

Also, for experienced readers, no wrong interpretation should make any sense. “ptr is N-aligned” can’t possibly mean “the alignment of the type of ptr is N” because pointers all have a size and alignment of size_of::<usize>() anyways. And to speak of the alignment of the place in memory where a concrete pointer value itself lives doesn’t make sense either in case you interpret that ptr is supposed to talk about the value of ptr. Though in case of “POINTER_PLACE_EXPRESSION as N-aligned” maybe there’s some ambiguity; the reference page in question disambiguates by also introducing the term “PLACE_EXPRESSION is based on a misaligned pointer”, though.

Back to possible clarifications... one thing that’s kinda missing is defining anywhere what “POINTER_VALUE is N-aligned” actually means, so that could probably be added.

For instance, if ptr has type *const S where S has an alignment of 8, then ptr must be 8-aligned (i.e. ptr as usize is a multiple of 8) or else (*ptr).f is "based on an misaligned pointer"

3 Likes