Ok this one might be simple but I don't have any experience with dealing unmanaged memory within C#. I got a structure in my project containing a fixed length array of four bytes:
[StructLayout(LayoutKind.Sequential)]
unsafe struct MessageHeader {
...
public fixed byte Prefix[4];
...
}
Now all I want to do is creating an object of type MessageHeader and assign some bytes to the prefix. I tried the following:
MessageHeader x;
unsafe {
fixed (byte* ptr = x.Prefix) {
Marshal.Copy(new byte[] { 128 , 64, 128, 64 }, 0, new IntPtr(ptr), 4);
}
...
}
However I got the following error: "You cannot use a fixed statement to take the address of an already fixed expression".
I tried to use it without fixed:
Marshal.Copy(new byte[] { 128, 64, 128, 64 }, 0, new IntPtr(x.Prefix), 4);
But this gives me the error of a possibly uninitialized struct. Shouldn't that be irrelevant on writing to an arbitrary unmanaged buffer?