Questions tagged [unsafe-pointers]

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].

230 questions
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
  • 10,239
  • 7
  • 45
  • 74
52
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 Emre
  • 2,296
  • 1
  • 17
  • 20
51
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,719
  • 1
  • 22
  • 34
31
votes
4 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,150
  • 2
  • 12
  • 15
31
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
  • 7,828
  • 7
  • 38
  • 79
28
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
  • 68,014
  • 12
  • 81
  • 143
25
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
  • 734
  • 2
  • 9
  • 24
19
votes
3 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
  • 5,553
  • 6
  • 38
  • 67
17
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
  • 3,874
  • 7
  • 34
  • 59
17
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,137
  • 28
  • 52
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
  • 3
  • 14
13
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,109
  • 7
  • 43
  • 71
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
  • 6,993
  • 10
  • 39
  • 72
8
votes
2 answers

UnsafePointer from CGAffineTransform

I'm trying to create a CGPath in Swift. I'm using CGPathCreateWithRect(rect, transformPointer). How can I get an UnsafePointer from a CGAffineTransform? I've tried this: let transform : CGAffineTransform =…
Moshe
  • 55,729
  • 73
  • 263
  • 420
7
votes
1 answer

Getting value from unSafeMutablePointer Int16 in Swift for audio data purposes

I'm working to convert to Swift this code which helps get me get audio data for visualizations. The code I'm working with in Obj C, which works well, is: while (reader.status == AVAssetReaderStatusReading) { AVAssetReaderTrackOutput…
MScottWaller
  • 2,611
  • 16
  • 36
1
2 3
15 16