Instance Method

distance(to:)

Returns the distance from this pointer to the given pointer, counted as instances of the pointer’s Pointee type.

Declaration

func distance(to end: UnsafeMutableRawPointer) -> Int

Parameters

end

The pointer to calculate the distance to.

Return Value

The distance from this pointer to end, in strides of the pointer’s Pointee type. To access the stride, use MemoryLayout<Pointee>.stride.

Discussion

With pointers p and q, the result of p.distance(to: q) is equivalent to q - p.

Typed pointers are required to be properly aligned for their Pointee type. Proper alignment ensures that the result of distance(to:) accurately measures the distance between the two pointers, counted in strides of Pointee. To find the distance in bytes between two pointers, convert them to UnsafeRawPointer instances before calling distance(to:).