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
2
votes
1 answer
Implicit conversion from UnsafeBufferPointer to UnsafeRawBufferPointer
UnsafePointer can be implicitly cast to UnsafeRawPointers when passed as parameters:
var x = 42
func print(address p: UnsafeRawPointer, as type: T.Type) {
print(p.load(as: type))
}
withUnsafePointer(to: &x) { (ptr) in
print(address:…

ravron
- 11,014
- 2
- 39
- 66
2
votes
1 answer
String.withCString when the String is nil
The problem that'll be described relates to my previous question:
string.withCString and UnsafeMutablePointer(mutating: cstring) wrapped into a function which was my first approach to handle nil Strings (by putting withCString into a function)
and…

n.eesemann
- 43
- 4
2
votes
2 answers
string.withCString and UnsafeMutablePointer(mutating: cstring) wrapped into a function
I have a problem with the String.withCString{} in combination with UnsafeMutablePointer(mutating: ...).
Given: a C-Function like this
randomSign(xml_string: UnsafeMutablePointer!) -> Uint
Also given: a String like
str = "some xml_tag"
My…

n.eesemann
- 43
- 4
2
votes
1 answer
Why is using a pointer for a for loop more performant in this case?
I don't have a background in C/C++ or related lower-level languages and so I've never ran into pointers before. I'm a game dev working primarily in C# and I finally decided to move to an unsafe context this morning for some performance-critical…

Josh Alexander
- 53
- 5
2
votes
1 answer
Why does Swift "Managed.fromOpaque.takeUnretainedValue()" retain the value?
I was doing some tests with pointers in Swift, and wanted to verify that the objects that I was taking pointers of were not being retained.
func test1(){
let str = aaa();
print(CFGetRetainCount(str))
let ptr1 =…

user1122069
- 1,767
- 1
- 24
- 52
2
votes
2 answers
Swift 3 Compiler Error : 'bytes' is unavailable: use withUnsafeBytes instead
I have an encrypting method in Swift 2.2. How can I get bytes from data in Swift 3?
Here is my code:
func myEncrypt(encryptData:String) -> String? {
//security key must be 24charachters
let myKeyData : Data = "*******".data(using:…

ava
- 1,148
- 5
- 15
- 44
2
votes
1 answer
UnsafePointer in Swift 3
I know this question was asked several times but I really don't understand it.
I want to extract a value from a bluetooth device (miband).
In swift 2 it worked like this:
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic:…

Patricks
- 715
- 2
- 12
- 25
2
votes
2 answers
Convert NSUUID to UnsafePointer
Following the update to Swift 3, it appears both getUUIDBytes and getBytes are not available on the UUID object.
let uuid = UIDevice.current.identifierForVendor
let mutableUUIDData =…

ray
- 1,966
- 4
- 24
- 39
2
votes
1 answer
swift 3 how to use CFArrayCreate
I'd like to create a CFArray from an Array
previously in Swift 2, I'm able to do this:
let array = [0, 1, 2, 3]
let arrayPointer = UnsafeMutablePointer>(array)
let cfArray = CFArrayCreate(kCFAllocatorDefault, arrayPointer,…

gietal
- 129
- 10
2
votes
1 answer
Declaring a pointer to class-defined struct where the class is generic
When using the unsafe or fixed keyword in C#, you can define pointers to unmanaged types, like byte* int* etc. You can also define a pointer to any struct that only contains unmanaged types, for example:
namespace a
{
struct MyStruct
{
…

Reinstate Monica
- 588
- 7
- 21
2
votes
0 answers
EXEC_BAD_INSTRUCTION (code=1, address=0xe) with MusicSequenceBarBeatTimeToBeats in Swift 2
I have problem when using Apples MusicSequence C API in Swift 2. I can't figure out how to get rid of EXEC_BAD_INSTRUCTION when calling MusicSequenceBarBeatTimeToBeats.
I have tried a lot of different solutions found on internet, but nothing seems…

Gustaf
- 21
- 4
2
votes
1 answer
Inconsistencies when using UnsafeMutablePointer with String or Character types
I'm currently trying to implement my own DynamicArray data type in Swift. To do so I'm using pointers a bit. As my root I'm using an UnsafeMutablePointer of a generic type T:
struct DynamicArray {
private var root: UnsafeMutablePointer =…

Marcus Rossel
- 3,196
- 1
- 26
- 41
2
votes
1 answer
How to create a CIVector from array of CGFloat?
I am trying to create the CICrossPolynomial filter type in Swift.
I am unsure how to create the syntax to do it however.
The documentation specifies a CIVector which has an array of floats?
A CIVector object whose display name is…

Aggressor
- 13,323
- 24
- 103
- 182
2
votes
1 answer
'inout String' is not convertible to 'UnsafePointer'
I am using a function that takes an UnsafePointer.
How do I get an UnsafePointer from a String?
Trying &someString gives me the error:
'inout String' is not convertible to 'UnsafePointer'

JuJoDi
- 14,627
- 23
- 80
- 126
2
votes
2 answers
Error in Unsafe Code, reading memory using pointers
I have a binary serialized object in memory and I want to read it from memory by using pointers (unsafae code) in C#. Please look at the following function which is reading from memory stream.
static Results ReadUsingPointers(byte[] data)
{
…

ak1
- 387
- 7
- 20