• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: memmove in Swift
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: memmove in Swift


  • Subject: Re: memmove in Swift
  • From: Roland King <email@hidden>
  • Date: Thu, 21 Aug 2014 12:54:48 +0800

> On 21 Aug 2014, at 12:25 pm, Quincey Morris <email@hidden> wrote:
>
> On Aug 20, 2014, at 20:32 , Gerriet M. Denkmann <email@hidden> wrote:
>
>> But I thought that maybe memmove might be more efficient:
>>
>> let dest : UnsafeMutablePointer<Void> =  arr + lowerIndex + 1
>
> Er, no. There are multiple things wrong with this:
>
> — There’s nothing in the Swift array API contract that says that elements are stored in a contiguous block of memory, or even in several blocks of memory
>
> — Even if they were, there’s nothing that says how they’re laid out in that memory
>
> — Even if there was, there’s nothing that gives you access to that memory (well, AFAIK)
>
> — Even if there was, the use of ‘arr’ as a pointer to the start of the memory is a C-ism, and doesn’t apply to an array variable in Swift.
>
> If you want to do this sort of thing, you’re going to have to cause your own block of memory to be allocated (e.g. in a NSData object). Alternatively, you could code the move as you originally did, and trust that at some point the Swift compiler will understand what you’re doing and optimize it for you.
>
>

All that he said. Reinforcing the last point as well. There's really little point writing C in Swift.

And for completeness (surely you've found this by now) alt-click "Jump To Definition' on an Array takes you to a treasure trove of methods which will let you break yourself in 16 different ways. The second of these looked particularly promising as a wrecking ball.

extension Array {

    /// Call body(p), where p is a pointer to the Array's contiguous storage
    func withUnsafeBufferPointer<R>(body: (UnsafeBufferPointer<T>) -> R) -> R
    mutating func withUnsafeMutableBufferPointer<R>(body: (inout UnsafeMutableBufferPointer<T>) -> R) -> R
}

UnsafeMutableBufferPointers appear to have a way to get generally UnsafeMutablePointers out of them so that's one route to the raw memory corruption you seek.

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


References: 
 >memmove in Swift (From: "Gerriet M. Denkmann" <email@hidden>)
 >Re: memmove in Swift (From: Quincey Morris <email@hidden>)

  • Prev by Date: Re: memmove in Swift
  • Next by Date: Re: memmove in Swift
  • Previous by thread: Re: memmove in Swift
  • Next by thread: Re: memmove in Swift
  • Index(es):
    • Date
    • Thread