Layout of .NET value type in memory

前端 未结 3 1563
滥情空心
滥情空心 2021-01-01 23:03

I have the following .NET value types:

[StructLayout(LayoutKind.Sequential)]
public struct Date
{
    public UInt16 V;
}

[StructLayout(LayoutKind.Sequential         


        
3条回答
  •  粉色の甜心
    2021-01-01 23:42

    Two things:

    • StructLayout(Sequential) does not guarantee packing. You might want to use Pack=1, otherwise 32 and 64bit platforms might be different.

    • and string is a reference, not a pointer. If the string length is always fixed, you might want to use fixed char arrays:

      public struct MyArray // This code must appear in an unsafe block
      {
          public fixed char pathName[128];
      }
      

提交回复
热议问题