Hi, Ray. Most of the pointer model is covered in the original proposal for the 
UnsafeRawPointer type, SE-0107 
<https://github.com/apple/swift-evolution/blob/master/proposals/0107-unsaferawpointer.md>.
 That's a good place to find answers to pointer- and memory-related questions 
in general.

The short forms are "yes, binding memory to types is mostly about communication 
with the optimizer, but it may also be checkable at run-time some day", and 
"it's okay to skip 'deinitialize' and use assignment instead of 'initialize' if 
and only if the element type is trivial 
<https://github.com/apple/swift-evolution/blob/master/proposals/0107-unsaferawpointer.md#trivial-types>".

Additionally, as far as I know, mutation through UnsafeMutableRawBufferPointer 
doesn't count as binding any type to the memory, but Andy will have to check me 
on that.

Best,
Jordan


> On Dec 11, 2016, at 14:51, Ray Fix via swift-users <swift-users@swift.org> 
> wrote:
> 
> Hello,
> 
> So bindMemory is part of the memory model and memory can only bind to one 
> type at a time to avoid aliasing. But what does binding actually do? Is it 
> somehow communicating with the optimizer?
> 
> A tangentially related second question, in the following example:
> 
>   let count = 3
>   let mutablePointer = UnsafeMutablePointer<Int16>.allocate(capacity: count)
>   defer {
>     mutablePointer.deallocate(capacity: count)
>   }
> 
>   mutablePointer.initialize(to: 1234, count: count)
>   defer {
>     mutablePointer.deinitialize(count: count)  // must I do this?
>   }
> 
> Is it bad form if I don’t deinitialize and go straight to deallocate?  I can 
> see where it is important for ref types (to update ref counts, etc).  Is it 
> one of those things that the optimizer can remove for the case of value types?
> 
> Finally, if I initalize with some other means, such as with a raw buffer 
> pointer subscript, is there any need to deinitialize?  Can such memory be 
> considered initialized if I bind it with a type?
> 
>   // 1
>   let pointer = malloc(byteCount)
>   defer {
>     free(pointer)
>   }
> 
>   let mutableRawBufferPointer = UnsafeMutableRawBufferPointer(start: pointer, 
> count: byteCount)
>   
>   for index in mutableRawBufferPointer.indices {
>     mutableRawBufferPointer[index] = 42 + UInt8(index)
>   }
> 
> Perhaps there is a document or proposal somewhere that talks about these 
> things. Sorry if I missed it.
> 
> Thanks as always,
> Ray
> 
> 
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to