Skip to content

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

Merged
merged 1 commit into from
Aug 15, 2018

Conversation

frewsxcv
Copy link
Member

No description provided.

@frewsxcv
Copy link
Member Author

r? @rust-lang/docs

@GuillaumeGomez
Copy link
Member

Thanks!

r=me once CI is ok.

/// use std::alloc::{alloc, dealloc, Layout};
///
/// unsafe {
/// let layout = Layout::from_size_align(2, 1).unwrap();
Copy link
Member

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.

/// use std::alloc::{alloc_zeroed, dealloc, Layout};
///
/// unsafe {
/// let layout = Layout::from_size_align(2, 1).unwrap();
Copy link
Member

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>().

/// let layout = Layout::from_size_align(2, 1).unwrap();
/// let ptr = alloc(layout);
///
/// println!("Pointer {:p} has data: {:b}", ptr, *(ptr as *mut u16));
Copy link
Member

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.

@TimNN TimNN added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 14, 2018
@frewsxcv
Copy link
Member Author

@ollie27 Thanks for the feedback! Comments addressed in the latest force push

@ollie27
Copy link
Member

ollie27 commented Aug 15, 2018

Looks good now.

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Aug 15, 2018

📌 Commit d1193bf has been approved by ollie27

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 15, 2018
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Aug 15, 2018
Add doc examples for std::alloc::{alloc,alloc_zeroed}.

None
bors added a commit that referenced this pull request Aug 15, 2018
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
@bors bors merged commit d1193bf into rust-lang:master Aug 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants