【问题标题】:Marshall.sizeof and structure within C# applicationC# 应用程序中的 Marshall.sizeof 和结构
【发布时间】: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

我不明白这个结果,我需要知道:

  1. 结构属性的顺序是否会影响分配的字节数?
  2. 如果是这样,为什么?减少分配结构所需的字节数的最佳做法是什么?

【问题讨论】:

  • 我猜这与“填充”有关。在 x86 中我得到 12,12 而 x64 返回 16,24..

标签: c# .net memory-management struct marshalling


【解决方案1】:

您在 64 位模式下运行此代码,对象引用占用 8 个字节。 StructLayout.Pack 的默认值为 8,与大多数 C 编译器的默认设置相匹配。它确保结构的成员对齐到一个成员大小的倍数的地址。使代码快速运行并保持对变量原子更新的重要规则。它避免了处理器必须使用多个内存总线周期来访问变量值。

注释每个成员在为结构分配的内存中的存储位置:

    public struct PointA
    {
        public int x;             // Offset 0, 4 bytes
                                  // Offset 4, 4 bytes of padding
        public string posorneg;   // Offset 8, 8 bytes
        public bool isvalid;      // Offset 16, 4 bytes
                                  // Offset 20, 4 bytes of padding
    }                             // Total: 24 bytes
    public struct PointB
    {
        public bool isvalid;      // Offset 0, 4 bytes
        public int x;             // Offset 4, 4 bytes
        public string posorneg;   // Offset 8, 8 bytes
    }                             // Total: 16 bytes

需要插入前 4 个字节的填充以保持字符串引用成员与 8 对齐。最后 4 个字节的填充需要确保在将结构存储在数组中时字符串仍然对齐。

PointB 中根本不需要填充,所有内容都意外地正确排列。您可以很容易地看到 Pack 属性的效果,将 [StructLayout(LayoutKind.Sequential, Pack = 4)] 应用于结构,您会看到它们现在都占用 16 个字节。当然实际上不可能重新排序字段或修补包装,您需要匹配本机代码使用的结构的布局。

非常值得注意的是,CLR 会自动执行这种布局优化。在应用了[StructLayout(LayoutKind.Auto)] 的类或结构上,它重新排序 字段以获得最佳布局。托管代码的优点之一,使其与本机代码竞争。请记住,您从 Marshal 类中看到的内容仅适用于 类或结构被封送。内部布局是无法发现的,这就是 CLR 可以玩这些优化技巧的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-02
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多