File tree 2 files changed +12
-9
lines changed
2 files changed +12
-9
lines changed Original file line number Diff line number Diff line change 62
62
//! T` obtained from [`Box::<T>::into_raw`] may be deallocated using the
63
63
//! [`Global`] allocator with [`Layout::for_value(&*value)`].
64
64
//!
65
- //! For zero-sized values, the `Box` pointer still has to be [valid] for reads and writes and
66
- //! sufficiently aligned. In particular, casting any aligned non-zero integer literal to a raw
67
- //! pointer produces a valid pointer, but a pointer pointing into previously allocated memory that
68
- //! since got freed is not valid.
65
+ //! For zero-sized values, the `Box` pointer still has to be [valid] for reads
66
+ //! and writes and sufficiently aligned. In particular, casting any aligned
67
+ //! non-zero integer literal to a raw pointer produces a valid pointer, but a
68
+ //! pointer pointing into previously allocated memory that since got freed is
69
+ //! not valid. The recommended way to build a Box to a ZST if `Box::new` cannot
70
+ //! be used is to use [`ptr::NonNull::dangling`].
69
71
//!
70
72
//! So long as `T: Sized`, a `Box<T>` is guaranteed to be represented
71
73
//! as a single pointer and is also ABI-compatible with C pointers
Original file line number Diff line number Diff line change 20
20
//! be *dereferenceable*: the memory range of the given size starting at the pointer must all be
21
21
//! within the bounds of a single allocated object. Note that in Rust,
22
22
//! every (stack-allocated) variable is considered a separate allocated object.
23
- //! * Even for operations of [size zero][zst], the pointer must not be "dangling" in the sense of
24
- //! pointing to deallocated memory. However, casting any non-zero integer literal to a pointer is
25
- //! valid for zero-sized accesses. This corresponds to writing your own allocator; allocating
26
- //! zero-sized objects is not very hard. In contrast, when you use the standard allocator, after
27
- //! memory got deallocated, even zero-sized accesses to that memory are invalid.
23
+ //! * Even for operations of [size zero][zst], the pointer must not be pointing to deallocated
24
+ //! memory, i.e., deallocation makes pointers invalid even for zero-sized operations. However,
25
+ //! casting any non-zero integer *literal* to a pointer is valid for zero-sized accesses, even if
26
+ //! some memory happens to exist at that address and gets deallocated. This corresponds to writing
27
+ //! your own allocator: allocating zero-sized objects is not very hard. The canonical way to
28
+ //! obtain a pointer that is valid for zero-sized accesses is [`NonNull::dangling`].
28
29
//! * All accesses performed by functions in this module are *non-atomic* in the sense
29
30
//! of [atomic operations] used to synchronize between threads. This means it is
30
31
//! undefined behavior to perform two concurrent accesses to the same location from different
You can’t perform that action at this time.
0 commit comments