1

I'm getting the error below with this struct

Error Message:

"because it contains an object field at offset 4 that is incorrectly aligned or overlapped by a non-object field."

Struct:

public struct Buf_t
{
    [FieldOffset(0)]
    public ushort eventType;

    [FieldOffset(4)]
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1932)]
    public byte[] data;

    [FieldOffset(1936)]
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
    public byte[] heap;
}

Similar question has been asked here: Incorrectly aligned or overlapped by a non-object field error. But I'm not seeing DWORD boundary issue on mine. Not sure what I'm missing here.

HockChai Lim
  • 1,675
  • 2
  • 20
  • 30
  • 1
    Are you using 64 bits? It might happen that you need to align the array at an 8 byte boundary. – Alois Kraus Nov 29 '22 at 17:58
  • Looks like 64 bits might be the problem. It worked if I change the second offset to [FieldOffset(8)]. But I need to map this struct to a c API memory, not sure that is going to work correctly. – HockChai Lim Nov 29 '22 at 18:08
  • You can declare at Offset 4 a byte and get the pointer to it in a helper method which is a member of the class. Then you can copy the memory into a self allocated array just like the marshaller. Then you can add 3 more bytes and then an array of fixed size to fill the gap with 1930 bytes and then you can repeat the same game again to get the data of the second array. – Alois Kraus Nov 29 '22 at 18:29

0 Answers0