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
1
vote
1 answer
In Swift, how does one determine if two UnsafePointers reference the same memory?
I have two variables of type "UnsafePointer" that should point to two C arrays of floats.
I already know how to access the value of the memory to which they point.
What I don't know how to do, is determine if they both refer to the same…

original_username
- 2,398
- 20
- 24
1
vote
2 answers
Run a C# application, complied with "Allow unsafe code" setting, from a network location
My C# application uses pointers and hence is complied using the "Allow Unsafe Code" setting.
I know that it is quite difficult or not possible at all to run such an application from a network location.(or is there any way to run it??)
What I would…

aby sam ross
- 11
- 2
1
vote
1 answer
Cocoa Static Library – unsafe_unretained Property
I am creating my first static Cocoa Library, and I have noticed a curious thing compared to when I work with Cocoa Applications: When I add an IBOulet to a view in a xib file, the property is made unsafe_unretained by default instead of weak.
Is…

Michael Knudsen
- 629
- 1
- 7
- 23
1
vote
2 answers
dereference and advance pointer in one statement?
I'm reading from a byte array as follows:
int* i = (int*)p;
id = *i;
i++;
correct me if I'm wrong, but ++ has precedence over *, so is possible to combine the *i and i++ in the same statement? (e.g. *i++)
(this is technically unsafe C#, not C++, p…

toasteroven
- 2,700
- 3
- 26
- 35
1
vote
1 answer
Locked pointer when locking a rectangle in a C# Bitmap
This may be a repeat of the following unanswered question:
Help with bitmap lock - Format8bppIndexed
I'm locking an image in the following manner:
// PixelFormat is 8BppIndexed in my case.
Bitmap bmp = new Bitmap("mySampleImage.tif");
//…

Will Eddins
- 13,628
- 5
- 51
- 85
0
votes
1 answer
How to properly translate C code to UnsafeSwift
I'm learning about Unsafe Swift and trying to see if I can translate some C algorithms into it. I've been using some C algorithms in my Swift code for a while in order to get super fast runtime execution speeds for large data sets, but I'd love to…

RL2000
- 913
- 10
- 20
0
votes
0 answers
How to get C char * string value in swift?
char* iperf_get_test_json_output_string ( struct iperf_test* ipt ); // .h file
I am using iperf SDK .c file in iOS project in swift. Above function return (char *) pointer reference. I am getting pointer reference. Anyone Can help to get the…

Vikas Verma
- 21
- 3
0
votes
0 answers
DeviceIoControl (kernel32.lib)
Inspired by balena.io's drivelist, I create a Rust version. The idea is simple, using Windows' Setup API to read all connected storages list (with mount points/drive letters), using winapi crate
///file main.rs
use core::{slice, ffi};
use…

ir1keren
- 37
- 9
0
votes
1 answer
How do i pass uintptr to unsafe.Pointer() satisfying govet
I want to pass a uintptr to unsafe.Pointer but govet is telling me possible misuse of unsafe.Pointer. I can't figure out how to satisfy govet.
func Example(base uintptr) byte {
x := *(*byte)(unsafe.Add(base, 4))
return x
}
If i pass…

user19323515
- 47
- 4
0
votes
0 answers
Printing the real value of an UnsafeMutablePointer?
I'm trying to adapt code found here to make parsing dates more performant. Doing the following and then attempting to print the values is giving me massive numbers, rather than the underlying real value (I'm assuming it is a memory address).
private…

Karim
- 271
- 2
- 11
0
votes
1 answer
How to convert an UnsafeMutablePointer to a Data object
How do you convert an UnsafeMutablePointer to a Data object?
let bytes = UnsafeMutablePointer.allocate(capacity: 20)
let data: Data = ...?

Berry Blue
- 15,330
- 18
- 62
- 113
0
votes
0 answers
"Cgo argument has Go pointer to Go pointer" with sdl.PushEvent and non-empty struct
package main
import (
"unsafe"
"github.com/veandco/go-sdl2/sdl"
)
type MyType struct {
A int
}
func main() {
if err := sdl.Init(sdl.INIT_EVENTS); err != nil {
panic(err)
}
ty := sdl.RegisterEvents(1)
…

iBug
- 35,554
- 7
- 89
- 134
0
votes
0 answers
deleting pointer in c#
i wanted to run a c++ function in c# via dll and pass few doubles.
The easiest way i found to pass multiple values is to predefine pointers and safe the values there.
However, here comes the question do i have to free the memory in c# and if yes,…

HungryCroco
- 1
- 1
0
votes
1 answer
How to convert array to unsafe pointer
I am creating an app that tracks a drive, so as you drive it records location and at the end, it shows a map of where you have been. I am using swiftUI's MapKit to display map data. In the process of displaying the information, I ran into a…

a1cd
- 17,884
- 4
- 8
- 28
0
votes
0 answers
Pinning value types with GCHandle.Alloc
Is it possible to pin pass an int by reference to a method that will then pin that variable in memory so that I can access it with a pointer?
I'm trying to use GCHandle.Alloc to temporarily pin an int in memory so that I can safely store a pointer…

Kevin Howe
- 31
- 1
- 8