<div dir="ltr">What is the difference between:<div><div>ptr.storeBytes(of: x, toByteOffset: offset, as: type(of: x))</div><div>ptr.advanced(by: offset).assumingMemoryBound(to: type(of: x)).pointee = x</div></div><div>?</div><div>I noticed that the former traps if storing to a misaligned offset while the latter is happy to do that, and I saw it mentioned as a requirement in the documentation, but other than that I'm not sure what would be the pros and cons of using the former / latter?</div><div>/Jens</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Sep 23, 2016 at 10:23 PM, Joe Groff via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
> On Sep 23, 2016, at 1:55 AM, Gerriet M. Denkmann via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br>
><br>
><br>
>> On 23 Sep 2016, at 15:47, Gerriet M. Denkmann via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br>
>><br>
>> This used to work in Swift 2.2:<br>
>><br>
>> var bitfield: UnsafeMutablePointer<UInt8>?<br>
>> bitfield = UnsafeMutablePointer<UInt8>( malloc( 888 ) )<br>
>><br>
>> How is this written in Swift 3.0?<br>
><br>
> To answer my own question:<br>
> This works:<br>
> var bitfield: UnsafeMutableRawPointer<br>
> bitfield = UnsafeMutableRawPointer( malloc(888))<br>
><br>
> But then this stops working:<br>
> let theByte = self.bitfield[ 5 ]<br>
><br>
> Somehow the bitfield must know that it is a field of bytes (not shorts, ints or whatever). But how?<br>
<br>
</span>The RawPointer types provide methods that can load a value with a given offset and type for you. IIRC, `bitfield.load(fromByteOffset: 0, as: UInt8.self)` will do what you want.<br>
<br>
-Joe<br>
<div class="HOEnZb"><div class="h5">______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
</div></div></blockquote></div><br></div>