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
Understanding UnsafeRawPointer in objc_setAssociatedObject
I'd like to set n number of associated objects to an object in Swift
My understanding is the usual pattern for the UnsafeRawPointer reference is like this...
static var reference = "someRef"
public func add(to myObject: AnyObject) {
let adding…

Magoo
- 2,552
- 1
- 23
- 43
0
votes
1 answer
Assigning a manually allocated UnsafeMutablePointer
I would like to assign the pointee's value of a pointer to String that is manually allocated.
In other words, I would like to write something like that:
let pointer = UnsafeMutablePointer.allocate(1)
pointer.pointee = "Hello, World!"
//…

Alvae
- 1,254
- 12
- 22
0
votes
1 answer
Is it possible to get from Any type his UnsafeRawPointer and length?
I just want call C function
static inline uint64_t wyhash(const void* key, uint64_t len, uint64_t seed)
from Swift like that
func hash(key: T, seed: UInt64) -> UInt64 { wyhash(...) }
Is it possible? And how?

jeudesprits
- 45
- 1
- 4
0
votes
0 answers
Swift: EXC_BAD_ACCESS when accessing an external library
I'm using an external library written in C that applies filters to an image. It receives the original image pixels as an array of float values and writes the new image float values on another array. One specific filter creates a mask to be used by a…

georgebp
- 193
- 3
- 17
0
votes
2 answers
golang get char* as return value from dll
I'm using golang to call a Dll function like char* fn(), the dll is not written by myself and I cannot change it. Here's my code:
package main
import (
"fmt"
"syscall"
"unsafe"
)
func main() {
dll := syscall.MustLoadDLL("my.dll")
…

aj3423
- 2,003
- 3
- 32
- 70
0
votes
1 answer
How to get a pointer to the underlying value of an Interface{} in Go
I'm interfacing with C code in Go using cgo, and I need to call a C function with a pointer to the underlying value in an Interface{} object. The value will be any of the atomic primitive types (not including complex64/complex128), or string.
I was…

Will Da Silva
- 6,386
- 2
- 27
- 52
0
votes
2 answers
Need help to understand garbage collection in GoLang
I'm a little bit confused with GoLang's garbage collector.
Consider this following code, where I implement reader interface for my type T.
type T struct {
header Header
data []*MyDataType
}
func (t *T) Read(p []byte) (int, error) {
…

Ladislav Macoun
- 21
- 1
0
votes
0 answers
Pinning class instance with GCHandle.Alloc in C#
I'm working with an unmanaged C library which has a callback function that gets called from an internal thread in the C library. The callback has a void * 'context' parameter.
I want to set this context to the address of a C# class instance so that…

JPh
- 536
- 3
- 20
0
votes
1 answer
Type conversion from byte slice to struct with unsafe
I'm trying to understand why my code in Go doesn't work the way I thought it would. When I execute this test, it fails:
func TestConversion(t *testing.T) {
type myType struct {
a uint8
value uint64
}
myVar1 :=…

replay
- 3,569
- 3
- 21
- 30
0
votes
1 answer
Dereferencing raw pointer with explicit annotation in Rust
I was writing unsafe snippet of code to emulate how C would have allocated a memory for an int. The code allocates memory for an isize type, assign 0 to the pointer variable, then infinitely increment the value at the address every second
use…

Pandemonium
- 7,724
- 3
- 32
- 51
0
votes
1 answer
Use Java unsafe to point char array to a memory location
Some analysis on a Java application showed that it's spending a lot of time decoding UTF-8 byte arrays into String objects. The stream of UTF-8 bytes are coming from a LMDB database, and the values in the database are Protobuf messages, which is…

user1428945
- 339
- 1
- 13
0
votes
1 answer
c#: Copying between a double[] array and another array which is not a double[] using unsafe pointers
I have a rather strange problem that I cannot figure out. I am using a third party library that creates a buffer. This buffer can contain doubles but copying between a double array and it is extremely slow. There must be something going on behind…

Tom
- 527
- 1
- 8
- 28
0
votes
0 answers
Pointer to a global variable
So, I had a code like this (detail):
namespace itssafe
{
public unsafe partial class testing : Form
{
unsafe private static int original = 10;
unsafe private int tmp;
unsafe setter() // I'll use this several time
…

Gregori
- 85
- 7
0
votes
2 answers
Swift Mutating Data at Range
I am building a Data object that looks something like this:
struct StructuredData {
var crc: UInt16
var someData: UInt32
var someMoreData: UInt64
// etc.
}
I'm running a CRC algorithm that will start at byte 2 and process length 12.
When…

Brandon M
- 349
- 2
- 20
0
votes
1 answer
Copy a string into a struct of strings
I am trying to simulate the memcpy from C in Go using unsafe.Pointer.
I have to map a string into a struct of strings in the following way:
package main
import (
"fmt"
"unsafe"
)
type myMessage struct {
Field1 [30]string
Field2…

Chiara
- 17
- 2