What is the difference between: ptr.storeBytes(of: x, toByteOffset: offset, as: type(of: x)) ptr.advanced(by: offset).assumingMemoryBound(to: type(of: x)).pointee = x ? 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? /Jens
On Fri, Sep 23, 2016 at 10:23 PM, Joe Groff via swift-users < swift-users@swift.org> wrote: > > > On Sep 23, 2016, at 1:55 AM, Gerriet M. Denkmann via swift-users < > swift-users@swift.org> wrote: > > > > > >> On 23 Sep 2016, at 15:47, Gerriet M. Denkmann via swift-users < > swift-users@swift.org> wrote: > >> > >> This used to work in Swift 2.2: > >> > >> var bitfield: UnsafeMutablePointer<UInt8>? > >> bitfield = UnsafeMutablePointer<UInt8>( malloc( 888 ) ) > >> > >> How is this written in Swift 3.0? > > > > To answer my own question: > > This works: > > var bitfield: UnsafeMutableRawPointer > > bitfield = UnsafeMutableRawPointer( malloc(888)) > > > > But then this stops working: > > let theByte = self.bitfield[ 5 ] > > > > Somehow the bitfield must know that it is a field of bytes (not shorts, > ints or whatever). But how? > > 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. > > -Joe > _______________________________________________ > 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