0

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

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by __avd
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.