@RalfJung Notice that in this particular example the safety invariant on the pointer arithmetic operation is violated, and therefore, such programs have UB before the result of the arithmetic operations attempts to materialize a new pointer, and we therefore do not even guarantee that a pointer is produced at all.
@mcy
C does not go out of its way to say under what circumstances it considers memory as being initialized beyond some "implementation defined" handwaving.
C11 6.7.9.10 (initialization) is quite specific, and so is C11 J.2.1 (undefined behavior).
The issue is about when an lvalue can be created out of that pointer, and whether that lvalue can be coerced into a value. Creating a wild pointer is completely valid, but dereferencing it is not (since the pointer's bits are uninitialized).
C11 6.5.3.2.4 (address and indirection operators) is quite specific about that as well.
I think that "creating this type of pointer is ok by omission"
The C standard allows creating pointers from any bit-pattern (including uninitialized memory), and the different C operations on pointers have different pre-conditions on their arguments, e.g., *
requires the pointer to point to some memory (not be null or uninitialized), and for that memory to be a valid allocation, or else the behavior is undefined.
Rust specifies things with a similar structure as C: it specifies some general properties about pointers (layout, validity, safety, etc.), and then the different operations have extra safety requirements (e.g. ptr::read()
requirements are in broad strokes very similar to C's *
: if you want to read from a pointer, that pointer better be "readable").
Neither C nor Rust mention in the description of these operations the things that do not matter for their correctness (there is an infinite amount of such things, e.g., whether the allocation was performed via a Rust API, or whether today is Tuesday). From a spec point-of-view, I don't think mentioning these things is strictly necessary, although it is sometimes nice to have footnotes about this (we should try to make the Rust spec more "approachable" than C's, so that user can actually read it). There should definitely be some text introducing users to how memory works in Rust, and this should be definitely covered there. Maybe it might even be worth covering in the API docs of the raw pointer module?