public struct TestStruct
{
public int first;
public int second;
public int third;
}
Marshal.SizeOf returns 12, which is what I assume because the ints are 4 bytes each. If I were to change third to a double instead of an int, I would expect Marshal.SizeOf to return 16. It does. But if I were to add a fourth that was a double, Marshal.SizeOf returns 24, when I expect 20. I could have 10 ints and it would end up each int counted as 4bytes each. But if I add a double in after 3 ints, the size isn’t what I expect.
public struct TestStruct //SizeOf 12
{
public int first;
public int second;
public int third;
}
public struct TestStruct //SizeOf 16
{
public int first;
public int second;
public double third;
}
public struct TestStruct //SizeOf 24, but I feel like it should be 20
{
public int first;
public int second;
public double third;
public int fourth;
}
Where has my thinking led me astray?
In addition to JimR’s answer, you can explicitly change this behaviour using the StructLayout attribute:
You’ve told the compiler that this struct is sequentially laid out, and that the packing type should be 1, which means that all fields are laid out directly after each other. Pack 0 (the default) tells the compiler to use the current platforms padding.. while anything else (in multiples of 2) will lay it out in those specific boundaries. I would advise against this though.. it could become unpredictable depending on your usage.
For more info, see this MSDN article: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute.pack.aspx