[swift-users] Memory Address of value types and reference types

Quinn "The Eskimo!" eskimo1 at apple.com
Tue Sep 12 09:34:34 CDT 2017


On 12 Sep 2017, at 13:44, somu subscribe via swift-users <swift-users at swift.org> wrote:

> 1. Is the above shown the correct way to get reference type memory address ?
> 2. What Is it the correct way to get value type memory address ?

It’s hard to answer that without knowing how you’re intended to use these techniques.  If you can explain more about where you’re going with this, I should be able to make some specific recommendations.

For example, if you’re goal were to pass a Swift object to a C API that takes a callback function pointer and a ‘cookie’ value, and hen calls that function with that cookie (like the `qsort_r` function), the to/from opaque mechanism provider by `Unmanaged` is the recommended way to pass a Swift object through such a cookie value.

> 3. Is it possible to get the memory address of immutable value type (declared as let)

No, because such values don’t necessarily exist in memory.

Share and Enjoy
--
Quinn "The Eskimo!"                    <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware




More information about the swift-users mailing list