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
1 answer
How to invoke a mutating method on an unsafe pointer in Swift?
Having a struct with a mutating method I can pass an instance of a struct as inout parameter and call the method. The passed instance will be modified like shown in the example below.
struct S{
var i = 0
mutating func incI(){ i += 1}
}
func…

dastrobu
- 1,600
- 1
- 18
- 32
1
vote
1 answer
How to get a pointer of an interface
It's hard to explain, but how I can get a pointer of a something that implements some interface?
Consider the code below:
package main
import (
"fmt"
"unsafe"
)
type Interface interface {
Example()
}
type…

Inkeliz
- 892
- 1
- 13
- 25
1
vote
1 answer
'UnsafePointer' is not convertible to 'UnsafePointer<_>
I am trying to implement to write a wrapper around libssh2 using Swift. The following code is for removing a file via SFTP.
func removeFile(_ path: String) {
let data = path.data(using: String.Encoding.utf8)!
let result =…

Codey
- 1,131
- 2
- 15
- 34
1
vote
3 answers
Pass a pointer to an array section as a parameter in C#
I'm just learning neural networks and I would like to have the neuron's constructor receive a pointer to a section in an array that would be the chromosome. Something like this:
public int* ChromosomeSection;
public Neuron(int*…

Juan
- 15,274
- 23
- 105
- 187
1
vote
0 answers
How to convert unsafepointer to string in swift
I have C function that returns const char* and I am trying to print that variable in swift. In swift file, it says the returned value from C function is in UnsafePointer. How to I convert this to string so that I can print that variable?
This…

Joon Lee
- 61
- 3
1
vote
2 answers
Are these memory functions the same for Swift 5 Conversion UnsafeBufferPointer
I am doing some Swift 5 conversion on code that I don't quite understand, legacy code from a previous developer. I get:
'withUnsafeBytes' is deprecated: use withUnsafeBytes(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R` instead
for:
func…

Joshua Hart
- 772
- 1
- 21
- 31
1
vote
1 answer
How to store data from an UnsafeMutablePointer in the iOS file system
I am reading data from an MFi external device into a buffer using a 3rd party SDK "sessionController". See below:
let handle: UInt64 = self.sessionController.openFile(file.path, mode: openMode)
if handle == 0 {
//Error
…

Josh
- 2,547
- 3
- 13
- 28
1
vote
1 answer
Reason why Unsafe Pointers are used in Swift, especially in Metal
I have been looking into potential use cases of UnsafePointer and related UnsafeX in Swift, and am wondering what the use case is i Swift. It sounds like the main use case is performance, but then at the same time types are supposed to offer…

Lance
- 75,200
- 93
- 289
- 503
1
vote
1 answer
win.RegisterRawInputDevices Always Returns false
I'm using the https://github.com/lxn/win package to access low level Windows calls in Go. I'm calling win.RegisterRawInputDevices to register devices for raw input data, but it always returns false. I've done this in C# with no problems. Below is my…

John Boldt
- 91
- 7
1
vote
1 answer
How to copy memory to an UnsafeMutableRawPointer starting from an index in Swift?
I know how to copy memory from an array to an UnsafeMutableRawPointer starting at index 0 by using:
mutableRawPointer.copyMemory(from: bytes, byteCount: bytes.count * MemoryLayout.stride)
where bytes is an array of floats.
However, I would…

BroccoliFinancials
- 139
- 2
- 9
1
vote
1 answer
Casting UnsafeRawPointer to CGImage from C function that returns a CGImageRef Crashes Swift 4
We have a cross platform image processing library and there is a call that returns a CGImageRef as an UnsafeRawPointer which I need to turn into a CGImage. I know the call works as when using it in objective c I can just cast using (CGImageRef)…

Julian Hunt
- 119
- 3
- 11
1
vote
2 answers
Swift get data from UnsafeMutablePointer
I have a objc function like below
-(char *)decrypt:(char *)crypt el:(int)el{}
when I call this function from swift it returns UnsafeMutablePointer. Now I need to get the data from this pointer. The output should be a string like this…

udi
- 303
- 1
- 6
- 19
1
vote
1 answer
Cannot convert value of type 'UnsafePointer' to expected argument type 'UnsafePointer<_>'
I am trying to update the code from a CoreMidi exemple I found at http://mattg411.com/swift-coremidi-callbacks/
And the code is date to before Swift 3, so I need to make some ajustement.
Problem is I have basically never needed to play with unsafe…

Pascale Beaulac
- 889
- 10
- 28
1
vote
1 answer
What is the most efficient and portable way to define an order on pointers?
I have a slice that contains pointers to values. In a performance-critical part of my program, I'm adding or removing values from this slice. For the moment, inserting a value is just an append (O(1) complexity), and removal consists in searching…

Fabien
- 12,486
- 9
- 44
- 62
1
vote
1 answer
Writing to value at UnsafePointer
From what I understood, an UnsafePointer presents the pointee as immutable and an UnsafeMutablePointer presents the pointee as mutable. But the signature for the vDSP function vDSP_zrvmul is as follows:
func vDSP_zrvmul(_ __A:…

Rogare
- 3,234
- 3
- 27
- 50