Skip to content

Update UnsafeRawPointer proposal #507

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 30, 2016

Conversation

natecook1000
Copy link
Member

Updates the examples to use the new MemoryLayout APIs and fixes a
few typos.

Updates the examples to use the new MemoryLayout APIs and fixes a
few typos.
@natecook1000
Copy link
Member Author

@atrick Can you review?

let p = UnsafeMutableRawPointer.allocate(bytes: sizeof(Int.self),
alignedTo: alignof(Int.self))
let p = UnsafeMutableRawPointer.allocate(bytes: MemoryLayout<Int>.stride,
alignedTo: MemoryLayout<Int>.alignment)

initRawAorB(p, isA: true)
printA(p.assumingMemoryBound(to: A.self))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atrick Calling initRawAorB(p, isA: true) and then initRawAorB(p, isA: true) in this function violates the stated preconditions for initializeMemory(as:to:), since the second call initializes memory for B that is already initialized for A. Is this permitted because A is a trivial type and A and B are layout compatible, or is deinitialization required here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok to reinitialize trivial types. It doesn't even matter here that they are layout compatible (as long as the allocated memory is large enough and properly aligned). I think there was initially some concern about exposing the term "trivial type" in these doc comments. But that cat is out of the bag.

I'll fix the doc comments:
swiftlang/swift#4560

@atrick
Copy link
Contributor

atrick commented Aug 30, 2016

Awesome. Looks great!

@jtbandes jtbandes merged commit a5afa1e into swiftlang:master Aug 30, 2016
@araykopotay
Copy link

a5afale

@natecook1000 natecook1000 deleted the nc-pointers branch April 22, 2022 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants