Memory Address of value types and reference types

Hi,

I would like to know the correct way to get the memory address of value types and reference types.

Given below are the attempts made.

class C {}
struct S {}

let c1 = C()
var s1 = S()
var s2 = s1

//Reference type:
print("c1 address: \(Unmanaged.passUnretained(c1).toOpaque())") //Is this correct ?

//Value type:
withUnsafeMutablePointer(to: &s1) {
    print("s1 address: \($0)")
}

withUnsafeMutablePointer(to: &s1) {
    print("s1 address: \($0)")
}

//Output:
//c1 address: 0x000060c0000015f0
//s1 address: 0x0000000119027a90
//s1 address: 0x0000000114571880

The address of s1 seems to be different the 2 times it is printed. It seems like my approach isn’t correct.

Tested on:
Swift 4
Xcode 9.0 beta 6 (9M214v)

Questions:
1. Is the above shown the correct way to get reference type memory address ? If not could you let me know the correct way.
2. What Is it the correct way to get value type memory address ?
3. Is it possible to get the memory address of immutable value type (declared as let)

Thanks and regards,
Muthu

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

···

On 12 Sep 2017, at 13:44, somu subscribe via swift-users <swift-users@swift.org> wrote:
--
Quinn "The Eskimo!" <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

Hi Quinn,

Thanks for the reply,

It is an iOS Swift project (uses Foundation, UIKit, CloudKit and other native frameworks) in which I would like to check the memory address for debugging (and out of enthusiasm). There is no C code I am using.

I have some asynchronous call back functions from CloudKit frameworks which return CKUserIdentity objects.

So thought it would be nice if I could print the memory address of CKUserIdentity objects and to check if it was unique.

And there are some other custom Swift Structs which I would like to know the memory address of.

Thanks and regards,
Muthu

···

On 12 Sep 2017, at 10:35 PM, Quinn The Eskimo! via swift-users <swift-users@swift.org> wrote:

On 12 Sep 2017, at 13:44, somu subscribe via swift-users <swift-users@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

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Hi Quinn,

Thanks for the reply,

It is an iOS Swift project (uses Foundation, UIKit, CloudKit and other native frameworks) in which I would like to check the memory address for debugging (and out of enthusiasm). There is no C code I am using.

I have some asynchronous call back functions from CloudKit frameworks which return CKUserIdentity objects.

So thought it would be nice if I could print the memory address of CKUserIdentity objects and to check if it was unique.

And there are some other custom Swift Structs which I would like to know the memory address of.

Thanks and regards,
Muthu

For classes, use the Unmanaged API as Quinn’s suggested.

Your structs, tuples, and enums only have an address during mutation. So, for example, if you wrap all of your code in a function that takes the variable `inout`, you’ll see a consistent address within a single call to that function. There’s an implicit cast from `inout` to Unsafe[Mutable]Pointer arguments, so you can inspect the pointer value...

func foo(p: Unsafe[Mutable]Pointer) { print(p) }

foo(&s2)

As you noticed, between calls to `foo` you could see a different address.

If you really want to give your structs an “identity” you would need to wrap them in a class.

-Andy

···

On Sep 12, 2017, at 9:55 AM, somu subscribe via swift-users <swift-users@swift.org> wrote:

On 12 Sep 2017, at 10:35 PM, Quinn The Eskimo! via swift-users <swift-users@swift.org> wrote:

On 12 Sep 2017, at 13:44, somu subscribe via swift-users <swift-users@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!" <Apple Developer>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

_______________________________________________
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

Hi Quinn,

Thanks for the reply,

It is an iOS Swift project (uses Foundation, UIKit, CloudKit and other native frameworks) in which I would like to check the memory address for debugging (and out of enthusiasm). There is no C code I am using.

I have some asynchronous call back functions from CloudKit frameworks which return CKUserIdentity objects.

So thought it would be nice if I could print the memory address of CKUserIdentity objects and to check if it was unique.

And there are some other custom Swift Structs which I would like to know the memory address of.

Thanks and regards,
Muthu

For classes, use the Unmanaged API as Quinn’s suggested.

If you really just want a unique value and don't want to do anything with that value, ObjectIdentifier is even easier to use.

Jordan

···

On Sep 12, 2017, at 10:20, Andrew Trick via swift-users <swift-users@swift.org> wrote:

On Sep 12, 2017, at 9:55 AM, somu subscribe via swift-users <swift-users@swift.org> wrote:

Your structs, tuples, and enums only have an address during mutation. So, for example, if you wrap all of your code in a function that takes the variable `inout`, you’ll see a consistent address within a single call to that function. There’s an implicit cast from `inout` to Unsafe[Mutable]Pointer arguments, so you can inspect the pointer value...

func foo(p: Unsafe[Mutable]Pointer) { print(p) }

foo(&s2)

As you noticed, between calls to `foo` you could see a different address.

If you really want to give your structs an “identity” you would need to wrap them in a class.

-Andy

On 12 Sep 2017, at 10:35 PM, Quinn The Eskimo! via swift-users <swift-users@swift.org> wrote:

On 12 Sep 2017, at 13:44, somu subscribe via swift-users <swift-users@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

_______________________________________________
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

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

i think this is something that’s mostly important for educational purposes and the fact that Swift is mostly opaque to memory makes it harder to teach.

···

On Sep 12, 2017, at 12:19 PM, Andrew Trick via swift-users <swift-users@swift.org> wrote:

On Sep 12, 2017, at 9:55 AM, somu subscribe via swift-users <swift-users@swift.org> wrote:

Hi Quinn,

Thanks for the reply,

It is an iOS Swift project (uses Foundation, UIKit, CloudKit and other native frameworks) in which I would like to check the memory address for debugging (and out of enthusiasm). There is no C code I am using.

I have some asynchronous call back functions from CloudKit frameworks which return CKUserIdentity objects.

So thought it would be nice if I could print the memory address of CKUserIdentity objects and to check if it was unique.

And there are some other custom Swift Structs which I would like to know the memory address of.

Thanks and regards,
Muthu

For classes, use the Unmanaged API as Quinn’s suggested.

Your structs, tuples, and enums only have an address during mutation. So, for example, if you wrap all of your code in a function that takes the variable `inout`, you’ll see a consistent address within a single call to that function. There’s an implicit cast from `inout` to Unsafe[Mutable]Pointer arguments, so you can inspect the pointer value...

func foo(p: Unsafe[Mutable]Pointer) { print(p) }

foo(&s2)

As you noticed, between calls to `foo` you could see a different address.

If you really want to give your structs an “identity” you would need to wrap them in a class.

-Andy

On 12 Sep 2017, at 10:35 PM, Quinn The Eskimo! via swift-users <swift-users@swift.org> wrote:

On 12 Sep 2017, at 13:44, somu subscribe via swift-users <swift-users@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

_______________________________________________
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

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Thanks a lot Andrew and Quinn,

After your explanation, I understand the following better now:

- It makes sense to get the memory address of a reference type as it represents identity. Just realised there is also === identity operator to check if it is the same reference.

- The need to get the memory address of a value type might not be of the same significance (as it is a value) and the emphasis would be more on the equality of the value.

- And model based on whether the entity represents identity / value

Thanks and regards,
Muthu

···

On 13 Sep 2017, at 1:19 AM, Andrew Trick <atrick@apple.com> wrote:

On Sep 12, 2017, at 9:55 AM, somu subscribe via swift-users <swift-users@swift.org> wrote:

Hi Quinn,

Thanks for the reply,

It is an iOS Swift project (uses Foundation, UIKit, CloudKit and other native frameworks) in which I would like to check the memory address for debugging (and out of enthusiasm). There is no C code I am using.

I have some asynchronous call back functions from CloudKit frameworks which return CKUserIdentity objects.

So thought it would be nice if I could print the memory address of CKUserIdentity objects and to check if it was unique.

And there are some other custom Swift Structs which I would like to know the memory address of.

Thanks and regards,
Muthu

For classes, use the Unmanaged API as Quinn’s suggested.

Your structs, tuples, and enums only have an address during mutation. So, for example, if you wrap all of your code in a function that takes the variable `inout`, you’ll see a consistent address within a single call to that function. There’s an implicit cast from `inout` to Unsafe[Mutable]Pointer arguments, so you can inspect the pointer value...

func foo(p: Unsafe[Mutable]Pointer) { print(p) }

foo(&s2)

As you noticed, between calls to `foo` you could see a different address.

If you really want to give your structs an “identity” you would need to wrap them in a class.

-Andy

On 12 Sep 2017, at 10:35 PM, Quinn The Eskimo! via swift-users <swift-users@swift.org> wrote:

On 12 Sep 2017, at 13:44, somu subscribe via swift-users <swift-users@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

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

Thanks a lot Jordan,

ObjectIdentifier seems pretty cool, looks like a way of representing everything identifiable in Swift (AnyObject and Any.Type).

Easier to use and debug, would be perfect for what I am looking for !

The ObjectIdentifier documentation pretty much sums it up:
"In Swift, only class instances and metatypes have unique identities. There is no notion of identity for structs, enums, functions, or tuples."

class C {}
struct S {}

let c1 = C()
let c2 = c1

print("c1 address: \(Unmanaged.passUnretained(c1).toOpaque())")

let o1 = ObjectIdentifier(c1)
let o2 = ObjectIdentifier(c2)

print("o1 -> c1 = \(o1)")
print("o2 -> c2 = \(o2)")

if o1 == o2 {
    print("c1 = c2")
} else {
    print("c1 != c2")
}

let o3 = ObjectIdentifier(C.self)
let o4 = ObjectIdentifier(S.self)

print("o3 -> C = \(o3)")
print("o4 -> S = \(o4)")

//Output:
//c1 address: 0x00006040000165e0
//o1 -> c1 = ObjectIdentifier(0x00006040000165e0)
//o2 -> c2 = ObjectIdentifier(0x00006040000165e0)
//c1 = c2
//o3 -> C = ObjectIdentifier(0x00000001210b87f0)
//o4 -> S = ObjectIdentifier(0x00000001210b86c8)

Thanks and regards,
Muthu

···

On 13 Sep 2017, at 5:12 AM, Jordan Rose <jordan_rose@apple.com> wrote:

On Sep 12, 2017, at 10:20, Andrew Trick via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

On Sep 12, 2017, at 9:55 AM, somu subscribe via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hi Quinn,

Thanks for the reply,

It is an iOS Swift project (uses Foundation, UIKit, CloudKit and other native frameworks) in which I would like to check the memory address for debugging (and out of enthusiasm). There is no C code I am using.

I have some asynchronous call back functions from CloudKit frameworks which return CKUserIdentity objects.

So thought it would be nice if I could print the memory address of CKUserIdentity objects and to check if it was unique.

And there are some other custom Swift Structs which I would like to know the memory address of.

Thanks and regards,
Muthu

For classes, use the Unmanaged API as Quinn’s suggested.

If you really just want a unique value and don't want to do anything with that value, ObjectIdentifier is even easier to use.

Jordan

Your structs, tuples, and enums only have an address during mutation. So, for example, if you wrap all of your code in a function that takes the variable `inout`, you’ll see a consistent address within a single call to that function. There’s an implicit cast from `inout` to Unsafe[Mutable]Pointer arguments, so you can inspect the pointer value...

func foo(p: Unsafe[Mutable]Pointer) { print(p) }

foo(&s2)

As you noticed, between calls to `foo` you could see a different address.

If you really want to give your structs an “identity” you would need to wrap them in a class.

-Andy

On 12 Sep 2017, at 10:35 PM, Quinn The Eskimo! via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

On 12 Sep 2017, at 13:44, somu subscribe via swift-users <swift-users@swift.org <mailto:swift-users@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

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users