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
3
votes
4 answers
c pointer decrement safe/unsafe?
analyzing some code:
static volatile UCHAR *pucSndBufferCur;
eMBErrorCode eMBRTUSend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
{
if( eRcvState == STATE_RX_IDLE )
{
/* First byte before the Modbus-PDU is the…

Mislab
- 53
- 4
3
votes
0 answers
Segfault In Unity From Marshal.AllocHGlobal
In Unity, I had a bitstream system which required constant pinning of arrays to work with. I'm optimising this out by allocating a block of unmanaged memory and maintaining pointers to it while the system is alive. A note in case this is suggested;…

Daedalus
- 86
- 10
3
votes
1 answer
Accessing "this" pointer/reference using an indexer in C#
I am experimenting with a datastructure for a performance / memory critical part of our codebase. I would like to have a fast access to the bytes defined in the structure. However I am not sure how to access the structure on which I am operating on…

Kraken
- 295
- 1
- 3
- 13
3
votes
1 answer
Assigning UnsafeMutablePointer value to UnsafePointer without unsafeBitCast
I am working with a C API that defines a structure with const char* and a function that returns a char* and am trying to find the best way to do the assignment.
Is there a way to do this without using unsafeBitCast? If I don't put the cast then I…

Julian
- 2,837
- 17
- 15
3
votes
1 answer
What difference in withUnsafePointer and Unmanaged.passUnretained
I want to see how swift String struct pointer changes when I append new string to original one, comparing to Objective C. For Objective C I use this code:
NSMutableString *st1 = [[NSMutableString alloc] initWithString:@"123"];
[st1…

Stanislav Chernischuk
- 975
- 1
- 11
- 24
3
votes
1 answer
'init is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type
since i converted my Code to Swift 3 the error occurs.
'init is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.
Here is my Code:
func parseHRMData(data : NSData!)
{
var flags :…

Florian Führer
- 55
- 1
- 7
3
votes
0 answers
Converting UnsafeBufferPointer to Data in Swift 3
I'm trying to initialize Data from an UnsafeBufferPointer, but it's throwing an EXC_BAD_ACCESS when it hits the third line. Help appreciated.
let pointer = UnsafePointer(UnsafePointer(bitPattern: 15)!)
let buffer =…

Mark Sands
- 1,509
- 1
- 14
- 15
3
votes
1 answer
How to pass an UnsafePointer to a C API by reference?
I wrote some Swift 2.2 code to interact with OpenSSL C APIs and now I'm trying to convert it to Swift 3.
In Swift 2
let octets = pkcs7_d_data(pkcs7_d_sign(receiptPKCS7).memory.contents)
var ptr = UnsafePointer(octets.memory.data)
// now…

ray
- 1,966
- 4
- 24
- 39
3
votes
0 answers
'init' is unavailable: use 'withMemoryRebound(to:capacity:_)'
Following Swift3, I'm getting an error on UnsafePointer. The debugger's proposed solution is not really clear enough to do something with though.
I know UnsafePointer has been raised in a previous question Issue with UnsafePointer in SQLite…

Shane O'Seasnain
- 3,534
- 5
- 24
- 31
3
votes
3 answers
Cast to different C struct unsafe pointer in Swift
I want to call the Posix socket functions socket and bind from Swift. socket is pretty easy—it takes Int32s, but bind is causing a problem, because I have a sockaddr_in pointer, but it wants a sockaddr pointer. In C, this would be a cast,…

Rob N
- 15,024
- 17
- 92
- 165
3
votes
1 answer
Do I need to release an UnsafeBufferPointer, or the UnsafePointer used at the buffer pointer's starting memory location?
Consider this extension on NSData which serializes an NSData object into a hex String:
extension NSData {
func base16EncodedString(uppercase uppercase: Bool = false) -> String {
let buffer = UnsafeBufferPointer(start:…

JAL
- 41,701
- 23
- 172
- 300
3
votes
2 answers
how to pass a swift Array as UnsafePointer argument in a function
I read that in order to pass a swift variable/constant to an argument that expects UnsafePointer can be done by using the 'inout' (ampersand) symbol prefixing the variable/constant.
So in the following code, I want to pass the 'colors' array as…

malena
- 798
- 11
- 26
3
votes
1 answer
'NSError' is not convertible to '@lvalue inout $T9' in Swift
so I'm trying to use a performRequestWithHandler block on a SLRequest object in my Swift iOS app and I can't deal with the NSError object. This is what how my code looks :
posts.performRequestWithHandler({(response:NSData!,…

Que20
- 1,469
- 2
- 13
- 27
3
votes
1 answer
Swift: gettimeofday and Unsafe Pointers
The code in Swift
...
var time:timeval?
gettimeofday(UnsafePointer, UnsafePointer<()>) // this is the method expansion before filling in any data
...
The code in Objective C
...
struct timeval time;
gettimeofday(&time, NULL);
...
I have…

solenoid
- 954
- 1
- 9
- 20
3
votes
2 answers
Go: convert unsafe.Pointer to function pointer and vice versa
In C you can put function pointers into an array of void pointers and convert them back to function pointers of any type:
extern int (*fn1)(void);
extern void (*fn2)(int);
void foo(void)
{
void *array[2];
int i;
/* implicit…

user3510167
- 303
- 3
- 8