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
0
votes
1 answer
Pointer to loop variable for range over map or slice of interface{}
I'm using go-hdf5 and I'm hitting a problem when trying to write attributes in a loop from a map.
The attributes are created correctly (correct name and datatype) but the written value is garbage.
The same code outside of the loop works fine. I…

Simon
- 31,675
- 9
- 80
- 92
0
votes
1 answer
Efficiently writing Int16 data to memory in Swift?
I have a memory reference, mBuffers.mData (from an AudioUnit bufferList), declared in the OS X and iOS framework headers as an:
UnsafeMutablePointer
What is an efficient way to write lots of Int16 values into memory referenced by this…

hotpaw2
- 70,107
- 14
- 90
- 153
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
- 2,178
- 3
- 25
- 42
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,016
- 1
- 6
- 15
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
- 7,210
- 10
- 45
- 76
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,836
- 3
- 22
- 33
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,311
- 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
- 1,106
- 1
- 12
- 27
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,892
- 1
- 18
- 20
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
- 259
- 2
- 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
- 47,267
- 10
- 52
- 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
- 13,320
- 37
- 106
- 184
-1
votes
1 answer
Casting []uint32 to []byte without copying in golang
I'm working on a processor simulator in golang (for educational purposes). I need a type for memory unit for addressing. It may contain either a slice of memory (memory type is []byte) or one or several registers (they have []uint32 type), must be…

Kirill Tseytin
- 9
- 1
-1
votes
1 answer
What is the modern canonical way to support converting ContiguousBytes into both Array and "not-Array"?
For example, without this second overload, loading an Array will yield "UnsafeRawBufferPointer.load out of bounds". Is there a way to handle both cases without overloads?
let bytes: [UInt8] = [1, 0, 1, 0]
bytes.load() as Int32 //…
user652038