从内存位置复制字节时是否需要固定结构?

Bri*_*anB 10 .net c# garbage-collection

我在C#中定义了一个结构来镜像本机数据结构并使用了Sequential的StructLayout.要将结构转换为Socket IOControl方法所需的12个字节(3x4​​个字节),我使用Marshal.Copy将字节复制到数组.

由于结构只包含值类型,在执行复制之前是否需要固定结构?我知道GC会压缩堆,因此在GC期间引用类型的mem地址可能会发生变化.堆栈分配值类型的情况是否相同?

包含引脚操作的当前版本如下所示:

[StructLayout(LayoutKind.Sequential, Pack = 1)]  
struct TcpKeepAliveConfiguration
{
        public uint DoUseTcpKeepAlives;
        public uint IdleTimeMilliseconds;
        public uint KeepAlivePacketInterval;

        public byte[] ToByteArray()
        {
            byte[] bytes = new byte[Marshal.SizeOf(typeof(TcpKeepAliveConfiguration))];
            GCHandle pinStructure = GCHandle.Alloc(this, GCHandleType.Pinned);
            try
            {
                Marshal.Copy(pinStructure.AddrOfPinnedObject(), bytes, 0, bytes.Length);
                return bytes;
            }
            finally
            {
                pinStructure.Free();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

Fré*_*idi 8

如果你的结构被一个lambda表达式捕获,它将不会存储在堆栈中.

因此,我建议您在复制前始终固定结构.

Eric Lippert撰写了一篇关于值类型存储文章,您可能会感兴趣.


Eri*_*ert 7

Frédéric和Aliostad是正确的; 你不知道"这个"实际存在的地方,因此你不知道是否允许垃圾收集器移动它.

我只是想指出,您可能会发现有用的问题的等效解决方案.您还可以使用以下方法解决问题:

public byte[] ToByteArray()
{
  byte[] bytes = new byte[Marshal.SizeOf(typeof(TcpKeepAliveConfiguration))];
  unsafe
  {
    fixed (TcpKeepAliveConfiguration* ptr = &this)
    {
      // now you have pinned "this" and obtained a pointer to it in one step
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

"fixed"语句确保在其块体内,"this"的非托管指针有效,因为垃圾收集器无法移动内存.基本上它是编写代码的另一种方式; 有些人发现这种方式更容易阅读.

(请注意,在构建包含不安全上下文的代码时,必须在Visual Studio中选中"允许不安全"复选框,或在命令行中使用"/ unsafe"标志.)