Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Why do i need to convert struct array to intptr?


Asked by Nova Bishop on Dec 12, 2021 FAQ



It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later. According to your description, you want to marshal an array of struct to IntPtr and then convert the IntPtr to type of struct array.
Furthermore,
In fact there is no need to do so much bit moving, as it is fairly easy to move the bytes in the struct directly to the byte array without the need for an intermediate buffer. The key to this generally useful technique is the GCHandle object. This will return a Garbage Collection handle to any managed data type.
In fact, You can find the full code of it on my GitHub – converting an array to a 16 byte struct one million times takes 3.85 seconds. This means that a single call to either of our methods takes less than 5 microseconds. That is pretty good!
Also Know,
Until C# 2.0 the main use of an Explicit layout was to provide fixed length buffers for use in DLL calls, for example. You simply cannot declare a fixed size array within a struct because initialising fields isn’t permitted. That is: …generates an error.
Besides,
Fastest is to pin the byte array in a fixed statement as proposed above in Tyalis’ answer. In some cases you can use an Int32 type (or Int64) in case of the IntPtr.