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

250 questions
5
votes
1 answer

Weak/Strong Reference Pointer Relationship

I have been attempting to write my own weak/strong pointer's but I am not clearly understanding the relationship. Everything I seem to come across does not make it and clear and quite often one doc will contridict what another doc says. Could anyone…
chadb
  • 1,138
  • 3
  • 13
  • 36
5
votes
1 answer

Swift 4.1 deinitialize and deallocate(capacity:) deprecated

I've been saying this to form a C array of CGPoint: let arr = UnsafeMutablePointer.allocate(capacity:4) defer { arr.deinitialize() arr.deallocate(capacity:4) } arr[0] = CGPoint(x:0,y:0) arr[1] = CGPoint(x:50,y:50) arr[2] =…
matt
  • 515,959
  • 87
  • 875
  • 1,141
5
votes
3 answers

A swiftier way to convert String to UnsafePointer in Swift 3 (libxml2)

I'm working on a Swift 3 wrapper for the libxml2 C-library. There are two convenience methods to convert String to UnsafePointer and vice versa. In libxml2 xmlChar is declared as unsigned char. UnsafePointer to String is…
vadian
  • 274,689
  • 30
  • 353
  • 361
5
votes
0 answers

Getting pointer to immutable structure

I'm writing wrapper for some C library. Is it possible to take UnsafePointer to immutable struct? For mutable structs it's not a problem: func Foo_get(ptr : UnsafePointer) { //does not mutate } struct Foo { //big data mutating…
szotp
  • 2,472
  • 1
  • 18
  • 21
5
votes
2 answers

Create a copy of CMSampleBuffer in Swift 2.0

This has been asked before, but something must have changed in Swift since it was asked. I am trying to store CMSampleBuffer objects returned from an AVCaptureSession to be processed later. After some experimentation I discovered that…
Rob
  • 4,149
  • 5
  • 34
  • 48
4
votes
1 answer

in golang, are unsafe.Pointer()s reference counted?

Are unsafe.Pointer()s reference counted and thus protected from being garbage collected? As in, if I have a string, it exists while it's in scope, if I have a *string, the string exists while the pointer is in scope, but if I have a…
stu
  • 8,461
  • 18
  • 74
  • 112
4
votes
1 answer

How to extend String's UnsafePointer initializer to accept NULL pointers?

In Swift, there's a String initialiser that takes an UnsafePointer (or UnsafePointer which is the same I think) as an argument. However, there's no initialiser that takes an optional UnsafePointer, e.g. a pointer that's NULL (or nil,…
j3141592653589793238
  • 1,810
  • 2
  • 16
  • 38
4
votes
1 answer

Passing nil as a UnsafePointer

In Swift 2.2, I was able to pass in nil as a valid parameter to a function which requires an UnsafePointer. In Swift 3, I can no longer do that: func myFuncThatTakesAPointer(buffer: UnsafePointer, length: Int) { /** **/…
JAL
  • 41,701
  • 23
  • 172
  • 300
4
votes
1 answer

Swift 3 - Convert c structure sockaddr_in to CFData

I need your help, this code does not compile anymore in Swift 3 //: Let's set up the `sockaddr_in` C structure using the initializer. var sin = sockaddr_in( sin_len: UInt8(sizeof(sockaddr_in)), sin_family: sa_family_t(AF_INET), sin_port:…
4
votes
1 answer

Swift error: '&' used with non-inout argument of type 'UnsafeMutablePointer'

I'm trying to convert the following Objective-C code (source) from this -(CGRect) dimensionsForAttributedString: (NSAttributedString *) asp { CGFloat ascent = 0, descent = 0, width = 0; CTLineRef line = CTLineCreateWithAttributedString(…
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
4
votes
4 answers

Why string pointer position is different?

Why string pointer position is different each time I ran the application, when I'm using StringBuilder but same when I declare a variable? void Main() { string str_01 = "my string"; string str_02 = GetString(); unsafe { …
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
4
votes
13 answers

Should I rewrite my DSP routines in C/C++ or I'm good with C# unsafe pointers?

I'm currently writing a C# application that does a lot of digital signal processing, which involves a lot of small fine-tuned memory xfer operations. I wrote these routines using unsafe pointers and they seem to perform much better than I first…
Trap
  • 12,050
  • 15
  • 55
  • 67
3
votes
2 answers

Swift array address is not same as &array[0]

Document said: An in-out expression that contains a mutable variable, property, or subscript reference of type Type, which is passed as a pointer to the address of the left-hand side identifier. A [Type] value, which is passed as a pointer to the…
Kk Shinkai
  • 316
  • 2
  • 10
3
votes
3 answers

How do I convert an array of UnsafeMutablePointer to an array of Double?

I have created an array like this var outputReal = UnsafeMutablePointer.allocate(capacity: numeroDados) Now I need to convert that to an array of Double. I can convert that using something like this: var newArray : [Double] = [] for i in…
Duck
  • 34,902
  • 47
  • 248
  • 470
3
votes
1 answer

(*C.uchar)(&buffer[0]) vs (*C.uchar)(unsafe.Pointer(&buffer[0]))

We have a discussion here over using (or not) unsafe.Pointer for passing pointers to byte arrays from Go to C. What is the biggest reason (not) to use unsafe.Pointer() ? I would take consistency as a reason, as you'd call an 'external' function,…
Diamondo25
  • 769
  • 1
  • 8
  • 21
1 2
3
16 17