Some "safe" languages like Swift and Go offer "unsafe" pointers for use with APIs written in languages like C and Objective-C. Questions using this tag should also have the relevant language tag, e.g. [swift] or [go].
Questions tagged [unsafe-pointers]
250 questions
1
vote
3 answers
Possible Bug in Swift 3 with Implicit Bridging of multidimensional arrays?
I am currently working on a SpriteKit game written in Swift 3.
When I tried generating SKShapeNodes from points stored in a multidimensional array, using the init(splinePoints: UnsafeMutablePointer, count: Int) initialiser, I found that I just was…

Jeffrey Douglas
- 53
- 3
1
vote
2 answers
Fast way of converting a slice of type T, to a slice of byte in Go
I have slices of different types, and I need to send them with TCP. The length of my slices is large, and not a constant value. Is there any way that I can convert the slices into []byte without looping through individual elements? For example, I…

Ari
- 23
- 4
1
vote
1 answer
What is the correct way to reinterpret an entity in Swift?
There are cases when you have to deal with a structure of certain type, but the upstream API requires you to present it though a pointer to another type at other places.
For instance, Unix Bind is expecting its second argument to be a pointer to a…

Schmo
- 353
- 2
- 10
1
vote
0 answers
Creating NSData from unsafe pointer in Swift
What's the Swift equivalent of:
var ed = NSData(bytes: &e, length: sizeof(e))

mm24
- 9,280
- 12
- 75
- 170
1
vote
1 answer
C const void * param from Swift: Data?.withUnsafeBytes and UnsafeRawPointer
I'm trying to pass the bytes contained within a Data? to a C function. The C function is declared like:
void func(const void *buffer);
And my Swift looks like:
myData?.withUnsafeBytes { (buffer: UnsafeRawPointer) in
func(buffer)
}
However,…

craig65535
- 3,439
- 1
- 23
- 49
1
vote
1 answer
Can the `UnsafePointer` mechanism reveal an array of vectors in a Data buffer for use in SceneKit?
I have a binary file that contains many triples of Float values; it is actually a long array of SCNVector3 representing the vertices for a SCNGeometrySource. I read the file (excuse the omission of do-try-catch, etc) into memory:
let sceneVectors =…

Ramsay Consulting
- 593
- 1
- 5
- 14
1
vote
7 answers
How to convert List to Byte[] in C#
Convertion from Double[] src to Byte[] dst
can be efficiently done in C# by fixed pointers:
fixed( Double* pSrc = src)
{
fixed( Byte* pDst = dst)
{
Byte* ps = (Byte*)pSrc;
for (int i=0; i < dstLength; i++)
{
*(pDst + i) = *(ps…

user536443
- 11
- 3
1
vote
1 answer
Trouble converting extension on Data to Swift 3.1 and UnsafeRawPointer
I am updating a swift 2.3 project to 3.1 and I am running into an issue converting this function, specifically on one line.
This is in an extension on Data.
public func read(offset f_offset: inout Int,
…

Siriss
- 3,737
- 4
- 32
- 65
1
vote
1 answer
How do I access UnsafePointer of member field of a struct referenced by an UnsafePointer in Swift?
Given
public struct MIDIPacketList {
public var numPackets: UInt32
public var packet: (MIDIPacket)
}
I would like to access an UnsafeMutablePointer of packet, given I have an UnsafeMutablePointer. Merely getting a copy of the original…

Zsolt Szatmari
- 1,219
- 1
- 12
- 29
1
vote
1 answer
Convert pointers from Swift 2 to Swift 3
How can I convert the following pointers initialization from Swift 2 to Swift 3?
var values: [Double]
...
var valuesAsComplex : UnsafeMutablePointer?
values.withUnsafeBufferPointer { (resultPointer: UnsafeBufferPointer) ->…

codifilo
- 50
- 1
- 5
1
vote
1 answer
Swift conversion: ERROR - UnsafeMutablePointer
I am attempting to convert my Swift 2 code into the latest syntax(Swift 3). I am receiving the following error:
Cannot invoke initializer for type 'UnsafeMutablePointer' with an argument list of type…

Cari95
- 313
- 6
- 16
1
vote
1 answer
Swift 3.0 UnsafeMutableRawPointer in Case Switch
Following works in Swift 3.0:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
{
if context == &MyContext1 {
.........
}
…

Gizmodo
- 3,151
- 7
- 45
- 92
1
vote
2 answers
How to convert UnsafePointer<[Float]> or fit Float Array data to Unsafe[Mutable]Pointer in Swift 3?
I have an array of float value (floatArray) coming from HDF5 file, and I'd like to use this data as an input for the function that accepts Unsafe[Mutable]Pointer (inputForFunction). I'm new to UnsafePointer, so I need someone's help.
One way I have…

kangaroo
- 407
- 4
- 19
1
vote
2 answers
Using bytes in swift3
float *vertexBuffer = (float *)positionSource.data.bytes;
'bytes' is unavailable: use withUnsafeBytes instead
but I don't know how to use it
_ = positionSource?.data.withUnsafeBytes({ (<#UnsafePointer#>) -> ResultType in
})

HaoDong
- 339
- 1
- 10
1
vote
1 answer
In Xcode 8 (Swift 3) what does UnsafeMutablePointer<_> mean?
The following code:
var mutableDataP = UnsafeMutablePointer(audioBuffer.mData)
let stereoSampleArray = UnsafeMutableBufferPointer(
start: mutableDataP,
count: nBytesInBuffer/sizeof(Int16) // Int16 audio samples
)
gives…

RobertL
- 14,214
- 11
- 38
- 44