❔ How do I get an IntPtr from a Span [Answered]

a library wants me to give a span<byte> as a pointer, and I am not sure as I haven't really done much with pointers in c# before
9 Replies
Akseli
Akseli3y ago
ref byte managedPtr = ref MemoryMarshal.GetReference(span); for managed pointer byte* ptr = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(span)); for pointer alternative: byte* ptr = (byte*)Unsafe.AsPointer(ref span[0]); to get pointer at specific index for IntPtr IntPtr ptr = Unsafe.AsPointer(ref MemoryMarshal.GetReference(span)); or just ref span[index]
Sherbert Lemon
Sherbert LemonOP3y ago
lovely thanks
Accord
Accord3y ago
✅ This post has been marked as answered!
Sherbert Lemon
Sherbert LemonOP3y ago
waaait can i unclose
Akseli
Akseli3y ago
whats the question
Sherbert Lemon
Sherbert LemonOP3y ago
nah it is all good, it returned a void* but that can be implicitly casted to an intptr
reflectronic
reflectronic3y ago
all of those are very incorrect answers if you made the span from an array
Akseli
Akseli3y ago
use fixed in that case
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server