[swift-dev] What exactly does it mean for a Swift pointer to be initialized?

Jens Persson jens at bitcycle.com
Fri Aug 5 14:43:59 CDT 2016


I'm trying to understand the new Swift 3 (4?) pointer API and Swift's
memory model.

More specifically, I'd like to know more about what exactly it means for a
pointer to be initialized or not.

For example, I suppose the following code example doesn't satisfy the
precondition in the subscript documentation (ie floatsPtr not being
initialized when using its subscript):

let numFloats = 123
let floatsPtr = UnsafeMutablePointer<Float>.allocate(capacity: numFloats)
for i in 0 ..< numFloats { floatsPtr[i] = Float(i) * 0.1 } // Setting values
for i in 0 ..< numFloats { print(floatsPtr[i]) } // Getting values
floatsPtr.deallocate(capacity: numFloats)

I'd like to understand why/how this could lead to undefined behavior, and
what exactly it means for a pointer to be initialized or not.

I've read
https://github.com/apple/swift-evolution/blob/master/proposals/0107-unsaferawpointer.md

But I don't feel that I fully understand what it means for a pointer to be
initialized, or bound, and if the preconditions and rules for undef
behavior are the same no matter if Pointee is a trivial type or a class
type.

/Jens
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20160805/aad0ce74/attachment.html>


More information about the swift-dev mailing list