Proposal: allow pinning a generic ref T/T[] variable as either T* or void* #1234
Unanswered
Sergio0694
asked this question in
General
Replies: 2 comments
-
I think extending the ref Struct r1 = ...;
fixed(ref Struct r2 = ref r1) //pinned Struct&
{
int* ptr = &r2.Field; //"fixed" not needed since the object is already pinned
}
Class o1 = ...;
fixed(Class o2 = o1) //pinned Class
{
int* ptr = &o2.Field;
}
//maybe even these
string s = ...;
fixed(ref char rc = s)
//or
fixed(TypedReference tr = __makeref(...)) Generic pointers would require the type to be blittable (which needs implementing another feature), but this is not such a heavy change. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
C# currently allow to declare fixed
void*
pointers from basically anystruct
type (in a non-generic method), as well as typed pointers tostruct
types, but it isn't possible to get either aT*
pointer nor avoid*
pointer from aref T
orT[]
variable, even whenT : struct
. So, currently:But something like this will just fail:
It'd be great to have a way to be able to work with pointers on arrays of generic structs, with either one of these solutions:
T*
pointers when inside anunsafe
blockvoid*
pointers fromref T/T[]
variables when inside anunsafe
blockI'm aware that the first two would probably need changes to the C# compiler, and since they're not supported right now I assume that's on purpose, but it was worth asking.
Note:
I'm aware it's now possible to get a pinned
void*
pointer from aref T
value like this:But still, being able to convert a
ref T
with the&
operator directly to eithervoid*
orT*
would be better. Specifically, having aT*
pointer would make pointer operations way easier, since the bytes offset would be automatically handled when moving the pointer (as the size of T is known anyways, as it's just a struct).Thanks for your time! 😄
Beta Was this translation helpful? Give feedback.
All reactions