A pointer created from an arbitrary arithmetic expression may designate an invalid address,
including an address that does not point to a valid object, an address that points to an
object of the wrong type, or an address that is not properly aligned. Use of such a pointer
to access memory will result in undefined behavior.
Satisfying those address, type, and alignment conditions is not sufficient to establish that
memory access is permitted. A well-formed pointer may carry no provenance
[FLS-WELL-FORMED-POINTER], and accessing memory through a pointer without
provenance permitting the access is undefined behavior
[FLS-POINTER-ACCESS-PROVENANCE].
The FLS does not specify the provenance result of every numeric-to-pointer conversion. This
guideline therefore imposes a conservative subset restriction instead of treating a matching
numeric address or pointer representation as proof that the result may be dereferenced.
The as operator also does not check that the size of the source operand is the same as
the size of a pointer, which may lead to unexpected results if the address computation was
originally performed in a differently-sized address space.
While as can notionally be used to create a null pointer, the functions
core::ptr::null and core::ptr::null_mut are the more idiomatic way to do this.
|