Does passing an array (or the address of an array) to a C API pass the address of contiguous (mutable) element storage?
The Swift Blog article says Interacting with C Pointers says that
An immutable array value can be passed directly as a const pointer, and a mutable array can be passed as a non-const pointer argument using the & operator,
with the example
let a: [Float] = [1, 2, 3, 4]
let b: [Float] = [0.5, 0.25, 0.125, 0.0625]
var result: [Float] = [0, 0, 0, 0]
vDSP_vadd(a, 1, b, 1, &result, 1, 4)
On the other hand, Calling Functions With Pointer Parameters is less clear to me:
When you call a function that is declared as taking an
UnsafePointer<Type>
argument, you can pass any of the following:
- A
[Type]
value, which is passed as a pointer to the start of the array.
The elements of an Array
need not be stored contiguously in memory. Is “pointer to the start of the array” here guaranteed to be a “pointer to contiguous element storage” or only a “pointer to the initial array element”?
In other words, is it really safe to pass an array directly to a C API, or should withUnsafe(Mutable)BufferPointer(_:)
be used?
I think it was said somewhere in this forum that passing an array (or the address of an array) to a C API passes the address of contiguous (mutable) element storage, but I cannot find that anymore. Some clarification would be appreciated.