3

Document said:

  • An in-out expression that contains a mutable variable, property, or subscript reference of type Type, which is passed as a pointer to the address of the left-hand side identifier.
  • A [Type] value, which is passed as a pointer to the start of the array.

But when I run the following code :

func print<Type>(unsafePointer pointer: UnsafePointer<Type>) {
    print("\(pointer) ==> \(pointer.pointee) : \(Type.self)")
}

var array = [1, 2, 3, 4]
print(unsafePointer: array)
print(unsafePointer: &array[0])

I get

0x0000000104204240 ==> 1 : Int
0x00007ffeefbff440 ==> 1 : Int

Why their addresses are different?

1

2 Answers 2

2

Here

print(unsafePointer: array)

a pointer to the first element of the array storage is passed to the function. And here

print(unsafePointer: &array[0])

the subscript operator is called on the array (returning an Int) and the address of that (temporary) integer is passed to the function, not the address where the original array element is stored.

That becomes more obvious if you call the functions twice:

var array = [1, 2, 3, 4]

print(unsafePointer: array)     // 0x00007ffeefbff2e0
print(unsafePointer: array)     // 0x00007ffeefbff2e0, same as previous address

print(unsafePointer: &array[0]) // 0x00007ffeefbff320
print(unsafePointer: &array[0]) // 0x00007ffeefbff340, different from previous address

In addition, passing an in-out expression to a function can make a temporary copy, see for example Swift: always copies on inout?.

-2

Arrays in Swift have value semantics, not the reference semantics of arrays in C or Objective-C. The reason you're seeing different addresses (and addresses at all) is that every time you pass the array as a parameter, you're actually telling Swift to bridge your Array struct to an instance of NSArray.

1
  • 2
    Array is bridged to NSArray if you pass it to an Objective-C function taking an NSArray parameter. But that is not what is happening here.
    – Martin R
    Aug 25, 2020 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.