【发布时间】:2014-08-22 09:54:11
【问题描述】:
我想在我的应用程序中使用Marshall.SizeOf() 方法
class Class1
{
static void Main()
{
Console.WriteLine("Number of bytes needed by a PointB object: {0}",
Marshal.SizeOf(typeof(PointB)));
Console.WriteLine("Number of bytes needed by a PointA object: {0}",
Marshal.SizeOf(typeof(PointA)));
Console.ReadKey();
}
public struct PointA
{
public int x;
public string posorneg;
public bool isvalid;
}
public struct PointB
{
public bool isvalid;
public int x;
public string posorneg;
}
}
我得到了结果:
Number of bytes needed by a PointB object: 16
Number of bytes needed by a PointA object: 24
我不明白这个结果,我需要知道:
- 结构属性的顺序是否会影响分配的字节数?
- 如果是这样,为什么?减少分配结构所需的字节数的最佳做法是什么?
【问题讨论】:
-
我猜这与“填充”有关。在 x86 中我得到 12,12 而 x64 返回 16,24..
标签: c# .net memory-management struct marshalling