C# 不安全的指针字段

Wol*_*ade 2 c# unsafe field

这是要断了吗?它编译得很好,但根据读数,我不确定它是否保证 _ptRef 将始终指向构造函数中引用的结构。

我猜“中断”是指……GC 会移动指针 (_ptRef) 指向的结构吗?

public unsafe class CPointType0
{
    private PointType0* _ptRef = null;

    public CPointType0(ref PointType0 spt)
    {
        fixed (PointType0 * pt = &spt)
        {
            _ptRef = pt;
        }
    }

...a bunch of property accessors to fields of _ptRef (accessed as return _ptRef->Thing) }
Run Code Online (Sandbox Code Playgroud)

场景是

-PointType0 是一个结构体。

- 数据结构中内存中的数百万个 PointType0。这些曾经是引用类型,但是内存开销太大了。

-A List 只有当搜索操作找到相关的 PointType0 时才返回,并且这个 List 被传递并在很多上操作。

Joe*_*orn 6

这不安全。

代码离开fixed块后,垃圾收集器可以自由地再次移动东西。你想在这里完成什么?您可能想使用列表中项目的索引而不是指针吗?