Marshal.SizeOf

  • Thread starter Thread starter XingZheng
  • Start date Start date
X

XingZheng

Hi
struct ABC
{
UInt32 a; //4 bytes
byte b; //1 byte
}//=5bytes

Why does Marshal.SizeOf(typeof(ABC)) return 8 ?
Thanks
 
Why does Marshal.SizeOf(typeof(ABC)) return 8 ?

Because of the default packing and alignment for structs. If you want
a 5 byte struct, you can add the attribute

[StructLayout(LayoutKind.Sequential, Size=5)]

or

[StructLayout(LayoutKind.Sequential, Pack=1)]



Mattias
 
Back
Top