<div dir="ltr"><div class="gmail_signature" data-smartmail="gmail_signature">I'm trying to understand the new Swift 3 (4?) pointer API and Swift's memory model.</div><div class="gmail_signature" data-smartmail="gmail_signature"><br></div><div class="gmail_signature" data-smartmail="gmail_signature">More specifically, I'd like to know more about what exactly it means for a pointer to be initialized or not.</div><div class="gmail_signature" data-smartmail="gmail_signature"><br></div><div class="gmail_signature" data-smartmail="gmail_signature"><div class="gmail_signature" data-smartmail="gmail_signature">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):</div><div class="gmail_signature" data-smartmail="gmail_signature"><br></div><div class="gmail_signature" data-smartmail="gmail_signature">let numFloats = 123</div><div class="gmail_signature" data-smartmail="gmail_signature">let floatsPtr = UnsafeMutablePointer<Float>.allocate(capacity: numFloats)</div><div class="gmail_signature" data-smartmail="gmail_signature">for i in 0 ..< numFloats { floatsPtr[i] = Float(i) * 0.1 } // Setting values</div><div class="gmail_signature" data-smartmail="gmail_signature">for i in 0 ..< numFloats { print(floatsPtr[i]) } // Getting values</div><div class="gmail_signature" data-smartmail="gmail_signature">floatsPtr.deallocate(capacity: numFloats)</div><div class="gmail_signature" data-smartmail="gmail_signature"><br></div><div class="gmail_signature" data-smartmail="gmail_signature">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.</div><div><br></div><div><div>I've read <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0107-unsaferawpointer.md">https://github.com/apple/swift-evolution/blob/master/proposals/0107-unsaferawpointer.md</a></div></div><div><br></div><div>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. </div><div><br></div><div>/Jens</div><div><br></div></div>
</div>