❔ 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
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]lovely thanks
✅ This post has been marked as answered!
waaait can i unclose
whats the question
nah it is all good, it returned a void* but that can be implicitly casted to an intptr
all of those are very incorrect answers if you made the span from an array
use
fixed
in that caseWas 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.