Hello,
I'm trying to create a structure in C# with byte alignment, which "include" unions as well.
The code is compiling, unfortunately I get an exception linked to data alignment : "it contains an object field at offset 10 that is incorrectly aligned or overlapped by a non-object field"
the code is something like this:

[StructLayout(LayoutKind.Explicit)]
public class A
{
[System.Runtime.InteropServices.FieldOffset(0)]
Byte b1;
[System.Runtime.InteropServices.FieldOffset(0)]
Byte b1_2;
[System.Runtime.InteropServices.FieldOffset(1)]
Byte b2;
[System.Runtime.InteropServices.FieldOffset(1)]
Byte b2_2;

...
[System.Runtime.InteropServices.FieldOffset(10)]
Byte[] bArray; // 20 bytes whatever allocated by the constructor

}

from what I understand, I should use the Sequential option for
LayoutKind (ie with Pack=2 ;-)), but in this case, I'm loosing the
possibility to have multiple fields at the same position.

Any idea regarding this ?

Regards,
samuel

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.