-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add doc examples for std::alloc::{alloc,alloc_zeroed}. #53344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
09d5b14
to
ced0b10
Compare
r? @rust-lang/docs |
Thanks! r=me once CI is ok. |
src/liballoc/alloc.rs
Outdated
/// use std::alloc::{alloc, dealloc, Layout}; | ||
/// | ||
/// unsafe { | ||
/// let layout = Layout::from_size_align(2, 1).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
u16
generally has an alignment of 2 not 1. Using Layout::new::<u16>()
would be better here though.
src/liballoc/alloc.rs
Outdated
/// use std::alloc::{alloc_zeroed, dealloc, Layout}; | ||
/// | ||
/// unsafe { | ||
/// let layout = Layout::from_size_align(2, 1).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, this should be Layout::new::<u16>()
.
src/liballoc/alloc.rs
Outdated
/// let layout = Layout::from_size_align(2, 1).unwrap(); | ||
/// let ptr = alloc(layout); | ||
/// | ||
/// println!("Pointer {:p} has data: {:b}", ptr, *(ptr as *mut u16)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is reading uninitialised memory which is Undefined Behaviour. I suggest writing a value to it first and then reading that value back.
ced0b10
to
d1193bf
Compare
@ollie27 Thanks for the feedback! Comments addressed in the latest force push |
Looks good now. @bors r+ rollup |
📌 Commit d1193bf has been approved by |
Add doc examples for std::alloc::{alloc,alloc_zeroed}. None
Rollup of 8 pull requests Successful merges: - #52453 (improve diagnostics for tests with custom return values) - #53271 (use ? to simplify `TransitiveRelation.maybe_map`) - #53279 (Extend documentation of `rustc_on_unimplemented`) - #53342 (fix error for unsized packed struct field) - #53344 (Add doc examples for std::alloc::{alloc,alloc_zeroed}.) - #53368 (Ignore test that fails on stage1) - #53388 (Fix links' color) - #53396 (Fix since of Iterator::flatten to be a proper semver) Failed merges: r? @ghost
No description provided.