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
62
votes
4 answers
Swift 5.0: 'withUnsafeBytes' is deprecated: use `withUnsafeBytes(...)
I previously used this code in Swift 4.2 to generate an id:
public static func generateId() throws -> UInt32 {
let data: Data = try random(bytes: 4)
let value: UInt32 = data.withUnsafeBytes { $0.pointee } // deprecated warning!
return…

Baran
- 2,710
- 1
- 23
- 23
60
votes
3 answers
Swift 3 UnsafePointer($0) no longer compile in Xcode 8 beta 6
My code snipet as follows …:
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
}
… does no longer compile with the following error which I don't…

Stéphane de Luca
- 12,745
- 9
- 57
- 95
53
votes
4 answers
How to cast self to UnsafeMutablePointer type in swift
Trying to pass "self" to a C function in swift, when calling following code:
var callbackStruct : AURenderCallbackStruct =
AURenderCallbackStruct.init(
inputProc: recordingCallback,
inputProcRefCon: UnsafeMutablePointer
…

Peter Peng
- 1,910
- 1
- 27
- 37
38
votes
6 answers
Warning: Initialization of 'UnsafeBufferPointer' results in a dangling buffer pointer
After update to Swift 5.2 / Xcode 11.4 got a warning to following code:
extension Data {
init(from value: T) {
var value = value
let pointer = UnsafeBufferPointer(start: &value, count: 1)
self.init(buffer: pointer)
…

Exey Panteleev
- 1,260
- 3
- 13
- 15
32
votes
3 answers
Converting an UnsafePointer with length to a Swift Array type
I'm looking for the simplest ways to achieve reasonable C interoperability in Swift, and my current block is converting an UnsafePointer (which was a const char *), into an [Int8] array.
Currently, I have a naïve algorithm that can take an…

Ephemera
- 8,672
- 8
- 44
- 84
30
votes
4 answers
How to access unexported struct fields
Is there a way to use reflect to access unexported fields in Go 1.8?
This no longer seems to work: https://stackoverflow.com/a/17982725/555493
Note that reflect.DeepEqual works just fine (that is, it can access unexported fields) but I can't make…

U Avalos
- 6,538
- 7
- 48
- 81
29
votes
3 answers
UnsafePointer initializer in Swift 3
I have a receipt validation class that is deprecated since Swift 3 has released. I fixed some issues, but I still have many ...
Here is the GitHub source code I used :…

GrayFox
- 824
- 2
- 10
- 27
29
votes
4 answers
How to get bytes out of an UnsafeMutableRawPointer?
How does one access bytes (or Int16's, floats, etc.) out of memory pointed to by an UnsafeMutableRawPointer (new in Swift 3) handed to a Swift function by a C API (Core Audio, etc.)

hotpaw2
- 70,107
- 14
- 90
- 153
19
votes
5 answers
Cannot assign value of type UnsafeMutablePointer ObjCBool in Swift
I'm unfamiliar with Objective C.
I'm using a private framework and need to be able to change one of the properties from within my Swift code.
The property is declared in Objective C this way:
@property (nonatomic, assign) BOOL *isSSNField;
in swift…

YichenBman
- 5,011
- 8
- 46
- 65
18
votes
1 answer
If a function returns an UnsafeMutablePointer is it our responsibility to destroy and dealloc?
For example if I were to write this code:
var t = time_t()
time(&t)
let x = localtime(&t) // returns UnsafeMutablePointer
println("\(x.memory.tm_hour): \(x.memory.tm_min): \(x.memory.tm_sec)")
...would it also be necessary to also do…

sketchyTech
- 5,746
- 1
- 33
- 56
15
votes
3 answers
Getting pointer for first entry in an array
I want to get pointer of first entry in the array. This is how I tried
int[] Results = { 1, 2, 3, 4, 5 };
unsafe
{
int* FirstResult = Results[0];
}
Get following compilation error. Any ideas how to fix it?
You can only take the address of…

imak
- 6,489
- 7
- 50
- 73
14
votes
2 answers
Cast a Swift struct to UnsafeMutablePointer
Is there a way to cast a Swift struct's address to a void UnsafeMutablePointer?
I tried this without success:
struct TheStruct {
var a:Int = 0
}
var myStruct = TheStruct()
var address = UnsafeMutablePointer(&myStruct)
Thanks!
EDIT: the…

popisar
- 379
- 2
- 3
- 15
10
votes
3 answers
How do I cast an __NSMallocBlock__ to its underlying type in Swift 3?
I had a trick to help test UIAlertController that worked in Swift 2.x:
extension UIAlertController {
typealias AlertHandler = @convention(block) (UIAlertAction) -> Void
func tapButtonAtIndex(index: Int) {
let block =…

Robert Atkins
- 23,528
- 15
- 68
- 97
9
votes
1 answer
Xcode 10 Swift build error: "Converting non-escaping value to 'T' may allow it to escape"
I'm using the Swift-VectorBoolean library, which is currently on Swift 3.2, not yet updated for Swift 4.2, but should still work on Xcode 10. Running this on Xcode 9, it works fine. On Xcode 10, it gives an error that I'm not sure how to fix. This…

Andrew
- 7,693
- 11
- 43
- 81
9
votes
1 answer
Swift UnsafeMutablePointer?> allocation and print
I'm new to swift and I have some difficulties to deal with pointers of unmanaged CFString (or NSString).
I'm working on a CoreMIDI project that implies usage of UnsafeMutablePointer?> as you can see in this function :
func…

SamT
- 528
- 4
- 14