Questions tagged [unsafe-pointers]

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].

250 questions
2
votes
1 answer

Is it legitimate to use LayoutKind.Explicit to get around using "unsafe" pointers?

I have code which does XOR on blocks of data using pointers, which is fast, but I'd like to get rid of the "unsafe" requirement on the assembly. If I change it to use LayoutKind.Explicit and overlay a "ulong[]" on top of a "byte[]", I'm basically…
Bryce Wagner
  • 2,640
  • 1
  • 26
  • 43
1
vote
1 answer

Reinterpreting UInt64 as structure

In my code I'm calculating an UInt64 value, then casting it like this: return *(Cell*)packedUInt64; Cell is a struct. Marshal.SizeOf(new Cell()) prints 8, so it should match an UInt64, but the cast above crashes with AccessViolationException. I…
Stefan Monov
  • 11,332
  • 10
  • 63
  • 120
1
vote
2 answers

Write/read memory using Unsafe in java

I know it is possible to write and read directly to memory by using Unsafe class within the jvm. Apart from this being really unsafe and somehow counterproductive, I was instead wondering who/what is taking charge of checking the boundaries of the…
Leonardo
  • 9,607
  • 17
  • 49
  • 89
1
vote
0 answers

Is this use of unsafe.Pointer OK?

To sum, as helpfully stated by user 9072997: Since unsafe.Pointer can convert between types that have the same underlying type, can unsafe.Pointer also convert between function types who's arguments & return values have the same underlying type? I…
nigel239
  • 1,485
  • 1
  • 3
  • 23
1
vote
1 answer

Cgo: Write memory in C allocated by Go

For a non-struct slice in Go a := []byte{0,1,2,3,4,5,6,7} And pass to C via unsafe.Pointer() C.some_method((*C.char)(unsafe.Pointer(&a[0])), C.int(len(a))) Is it safe to write anything into this pointer with caution on it's length? void…
Wilson Luniz
  • 459
  • 2
  • 7
  • 18
1
vote
0 answers

C# working with generic array using unsafe

I need to get a pointer e.g. of type byte* to an array of generic type. I found something but see pointer to a array of type generic?, nothing that solves my problem. My generic class doesn't have the where T: unmanaged restriction, but internally…
Nullator
  • 71
  • 2
1
vote
1 answer

How to safely get two mutable pointers to same int?

I would like to create a Rust semaphore wrapping the libc sem_post and sem_wait functions, both of which take a mutable int * parameter. This requires that the waiter and poster simultaneously have a mutable pointer to the same int. How can I…
ridiculous_fish
  • 17,273
  • 1
  • 54
  • 61
1
vote
1 answer

Is Microsoft the only one that can write trusted unsafe code?

I would like to write some unsafe C# code for the purpose of writing high-performance, low-level libraries. But I don't quite understand the limitations of where that code can run. Microsoft obviously writes libraries that use unsafe code, which are…
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
1
vote
1 answer

EXC_BAD_ACCESS when trying to pass and read UnsafeMutableRawPointer through VTDecompressionSessionDecodeFrame

I think this is more a pointer question than a VideoToolbox session. I'm trying to do something similar to How to calculate FPS of Hardware Decode on iOS 8 exactly? and pass some metadata through VTDecompressionSessionDecodeFrame's…
1
vote
1 answer

'Invalid utf-8 sequence of 1 bytes from index 1' and 'munmup_chunk(): invalid pointer' error in my Rust unit-tests

There`s only lib.rs file: use libc::{self, O_RDONLY, strerror, open, close, __errno_location, read}; use std::ffi::{CString, CStr}; use std::{fmt, process}; // File descriptor struct Fd { fd: i32, } impl Fd { fn new(fd: i32) -> Fd { …
Michail
  • 13
  • 4
1
vote
1 answer

Why can I pass [UInt8] type to the UnsafePointer type parameter?

We all know that [UInt8] type is not equal UnsafePointer, But why can I pass [UInt8] type to UnsafePointer type parameter: let p = UnsafeMutablePointer.allocate(capacity: 4) p.initialize(repeating: 0, count: 4) let ary:[UInt8]…
hopy
  • 559
  • 3
  • 18
1
vote
1 answer

Data withUnsafeBytes is deprecated

I got this code here from an app I am working on. I inherited this code when the BLE guy left the team. I am not good with low level stuff and Data stuff. I am UI/UX front end person, and now I do need to get my hands dirty. This code is now a bit…
1
vote
1 answer

Rust: how to tell the borrow checker that the move is depend on a bool?

The following code doesn't pass the borrow checker, for Label-A uses a value that consumed by Label-B, but the code is actually safe: the Label-A is guarded with processed which is only set if Label-B is run. How can I tell the compiler the…
Incömplete
  • 853
  • 8
  • 20
1
vote
1 answer

Convert Data into UnsafeRawPointer and vice versa in swift

I have been working on an app that uses Pencil Kit and I am trying to store a drawing on the canvas into a sqlite3 database. In order to do so, I had to convert the drawing(type: Data) into an UnsafeRawPointer. However, after the conversion, when I…
Clare
  • 71
  • 6
1
vote
0 answers

Using the Objective-C runtime in Swift to dynamically call Objective-C methods returns invalid UnsafePointer

I am attempting to use the Objective-C runtime in Swift to dynamically call Objective-C methods using method_getImplementation. My approach works well for methods that either return void or that return an object, but it breaks when attempting to…
S.Moore
  • 1,277
  • 17
  • 17