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
0
votes
1 answer

Error with fetching value from CFArray

I'm trying to fetch contacts from address book. My code is working properly in Objective C, but when I converted it to swift I'm getting error Could not cast value of type 'Swift.UnsafePointer<()>' to 'Swift.AnyObject' Below is my code var…
Ankita Shah
  • 1,958
  • 3
  • 20
  • 39
0
votes
0 answers

How do I read data into a data structure from a function that accepts an UnsafeMutablePointer?

I'm reading data from a socket with an NSInputStream. This works great when the data is read into an array of UInt8s with the following: var buffer = [UInt8](count: 1024, repeatedValue: 0) let len = inputStream.read(&buffer, maxLength:…
Epsilon
  • 1,076
  • 1
  • 6
  • 13
0
votes
1 answer

Converting C char array (unsafe pointer) to String

I have an UnsafeMutablePointer filled by a CoreFoundation method. If I NSLog it with %s placeholder, it outputs just fine. But if I try with Swift's print it just writes the memory address. Tried nearly everything... also I don't…
Teejay
  • 6,633
  • 9
  • 42
  • 70
0
votes
2 answers

How to assign (byte* + offset) to byte[]?

I have a struct: [StructLayout(LayoutKind.Sequential, Pack = 1)] public unsafe struct FieldIndex { public fixed byte Meta[16]; } The first byte is TypeCode. The remaining 15 bytes is a UTF8-encoded string. I'm using the following to fill the…
IAbstract
  • 18,848
  • 14
  • 81
  • 137
0
votes
1 answer

Why NSMutableData change address of pointer?

I want to quick preallocate memory and have it wrapped by NSMutableData but with access via pointer. So I have this: var vertex = UnsafeMutablePointer.alloc(numberOfVertex * 3) vertex.initialize(0) vertexData = NSMutableData(bytesNoCopy:…
John Tracid
  • 3,612
  • 1
  • 17
  • 29
0
votes
3 answers

Assign a reference of an integer in a class

Is it possible to pass an integer as reference at class initialization and safe the reference? class Foo { private int _refVal; public Foo(ref int val) { _refval = val; // saves the value, not the reference } } I could use…
chriszero
  • 1,303
  • 3
  • 13
  • 26
0
votes
2 answers

Objective-c pointers magic. Type protection

I have a dictionary. I extract one of its values as follows: NSString *magicValue= [filterDict valueForKey:[filterDict allKeys][0]]; [SomeClass foo: magicValue]; And foo is: - (void)foo:(NSString*)magicValue { NSLog("magicValue is…
h3dkandi
  • 772
  • 6
  • 25
0
votes
1 answer

Reading values from binary file whose types are known at runtime

I'm trying to read a series of values from a binary file, but I won't know what the value types are until runtime. Simplified example I have a binary file that is 10 bytes long. The bytes represent, in order, an int, a float, and a short. I don't…
Reticulated Spline
  • 1,682
  • 1
  • 14
  • 19
0
votes
0 answers

How to install a callback function in swift

I'm trying to use DiskArbitration and install a first callback function, which is DADiskAppearedCallback, but i am stuck at the DARegisterDiskAppearedCallback statement, with this error: 'DeviceManager' is not convertible to…
0
votes
1 answer

I cannot get the integer value from unsafeMutablePointer<32> in swift

I use swift and obj-c classes together in my project. And I declare this in obj-c header file. @property (nonatomic, copy) NSString *AppStoreId; I want to use this obj-c code in my swift class: NSDictionary *productParameters = @{…
simge
  • 249
  • 1
  • 5
  • 10
0
votes
3 answers

How to read string from pointer to buffer in C#

How can I read the error string in C# from this C++ dll call? // // PARAMETERS: // objptr // Pointer to class instance. // // pBuffer // Pointer to buffer receiving NULL terminated error message string. // If…
Santiago Corredoira
  • 40,844
  • 8
  • 48
  • 56
0
votes
1 answer

How can I improve this code and make it safe to use?

I have picked up this piece of code but as I understand it is unsafe code to use and may not be possible in WP7. Does anyone have an idea about how I can make this code safe to use, maybe with try/catch? GCHandle gch =…
Jason94
  • 12,424
  • 33
  • 99
  • 173
-1
votes
1 answer

Issue updating code to Swift 5.1 'withUnsafeBytes is deprecated: use withUnsafeBytes(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R instead'

I'm using a simple extension to pack and unpack data, the code below currently still works but it's throwing me a warning and I'd like to update it to prevent issues down the line. There are two similar questions asked about this, but I haven't been…
Prolix
  • 86
  • 6
-1
votes
1 answer

i dont get the unsafe pointer in swift

i use an api in swift that provide me a function to send data to some destination ,the function has a parameter of type UnsafeMutablePointer < astruct >. astruct is a structure and has three variable, first one is the data , that is of type…
-1
votes
1 answer

Strange behavior of UnsafeMutablePointer

I've built a little structure which holds a pseudo-C-String UnsafeMutablePointer. Now I wanted to add a way to get the length of the String, but something behaved very strange: struct Char { let value: CChar init(_ char: CChar) { …
Josef Zoller
  • 739
  • 7
  • 21
1 2 3
15
16