> On Nov 18, 2015, at 11:12 AM, Quincey Morris 
> <[email protected]> wrote:
> On Nov 18, 2015, at 10:57 , Jens Alfke <[email protected]> wrote:
>> 
>> Doesn’t the pointer become invalid as soon as myElement goes out of scope? 
>> (There’s a reason that type is called *Unsafe*Pointer…)
> 
> Yes, but so does &myArray[0] in the original code. According to your 
> hypothesis, it’s copied into a temporary, which also becomes invalid as soon 
> as the current scope ends.

It’s actually shorter than that; inout temporaries are valid just for the 
duration of the call.

Going back to your actual problem: “user data” parameters like this are really 
just pointer-sized opaque buckets of bits.  Assuming your array is, in fact, 
stably-indexed (and if it isn’t, passing a pointer won’t work either!), 
consider just passing an index as the user data; you can construct a pointer 
from an Int like this:
  UnsafePointer(bitPattern: i)
and get the Int back like this:
  unsafeBitCast(ptr, Int.self)

The fact that the latter is apparently the only interface for getting back to 
an Int is probably a bug.

John.
_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to