开发者

Copying data from byte[] to struct using pointers in C#

开发者 https://www.devze.com 2023-02-16 13:10 出处:网络
I have a byte array (开发者_StackOverflow中文版from a socket) that I would like to copy into a struct.

I have a byte array (开发者_StackOverflow中文版from a socket) that I would like to copy into a struct. I have moved away from using Marshal.Copy as this, to my understanding, allocates managed memory and not stack memory. (The data comes at a rather fast rate and have a short lifespan before being saved to disk, so I would like to avoid creating a lot of short lived managed objects.)

How can I copy, byte by byte, from the byte[] into the struct?


Final code looks like this:

unsafe void CastData(out SomeStructure someStruct, byte[] bytes)
{
    fixed (byte* pSource = bytes)
    {
        fixed (SomeStructure* pDest = &someStruct)
        {
            *pDest = *((SomeStructure*)pSource);
        }
    }
}

To my understanding, this should be completely deterministic and "safe".

Assuming that it is, then I found the unsafe method to be approx. 31 times faster than the method shown at (e.g.) http://thecodeisart.blogspot.com/2008/11/with-this-class-you-can-easy-convert.html

0

精彩评论

暂无评论...
验证码 换一张
取 消