Skip to content

Commit 21b502b

Browse files
committed
warn that raw pointers must be aligned when used, and that writes cause drop
1 parent e9d2227 commit 21b502b

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/libstd/primitive_docs.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,11 @@ mod prim_unit { }
362362
///
363363
/// *[See also the `std::ptr` module](ptr/index.html).*
364364
///
365-
/// Working with raw pointers in Rust is uncommon,
366-
/// typically limited to a few patterns.
365+
/// Working with raw pointers in Rust is uncommon, typically limited to a few patterns.
366+
/// Raw pointers can be unaligned or null when unused. However, when a raw pointer is used to
367+
/// load/store data from/to memory, they must be non-null and aligned.
368+
/// Storing through a raw pointer (`*ptr = data`) calls `drop` on the old value, so
369+
/// [`write`] must be used if memory is not already initialized.
367370
///
368371
/// Use the [`null`] and [`null_mut`] functions to create null pointers, and the
369372
/// [`is_null`] method of the `*const T` and `*mut T` types to check for null.
@@ -442,6 +445,7 @@ mod prim_unit { }
442445
/// [`offset`]: ../std/primitive.pointer.html#method.offset
443446
/// [`into_raw`]: ../std/boxed/struct.Box.html#method.into_raw
444447
/// [`drop`]: ../std/mem/fn.drop.html
448+
/// [`write`]: ../std/ptr/fn.write.html
445449
#[stable(feature = "rust1", since = "1.0.0")]
446450
mod prim_pointer { }
447451

0 commit comments

Comments
 (0)