Beorne said:
Jeroen said:
Beorne wrote:
[StructLayout(LayoutKind.Explicit)]
unsafe struct Structure
{
[FieldOffset(0)]
public byte b;
[FieldOffset(1)]
public double d;
}
Why Marshal.SizeOf(typeof(SPESharedRecord2)) has a value of 16 instead
of 9?
Because the structure is padded to platform alignment, even if you
explicitly lay out the fields. To suppress this, use
StructLayout(LayoutKind.Explicit, Pack = 1) and the struct will have the
expected size.
Forgot to add that if you do use Pack = 1, you don't need to lay out the
fields either in this case. LayoutKind.Sequential will have the same effect.
I'm using the Compact Framework that does not suport the Pack
attribute. Perhaps the problem is there ....
Yes, and that's a rotten situation to be in. Because you can't enforce
struct packing alignment, you have to do without it.
The next question would be: what do you need this struct for? In particular,
why do you need it to be packed? If push comes to shove, you can almost
always work around this by just not using structs. (If you're using .NET CF
2.0, however, your job is considerably harder.)
If you need to convert this struct to bytes to send it over the wire or to a
file somewhere, it's always an option to just send those bytes directly with
the help of BitConverter.
If you need to pass this struct to an API, then just passing it with extra
alignment will probably work, as long as you don't use Marshal.SizeOf() and
pass the desired size explicitly.