Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#不安全指针字段_C#_Field_Unsafe - Fatal编程技术网

C#不安全指针字段

C#不安全指针字段,c#,field,unsafe,C#,Field,Unsafe,这会打破吗?它编译得很好,但基于读数,我不确定它是否保证_ptRef始终指向构造函数中引用的结构 我猜“break”的意思是…GC会移动指针(_ptRef)指向的结构吗 情况是 -PointType0是一个结构 -数据结构中内存中有数百万个PointType0。这些过去是引用类型,但内存开销太大了 -仅当搜索操作找到相关的PointType0时,才会返回列表,并且此列表会被传递并对很多批次进行操作。这不安全 在代码离开fixed块后,垃圾收集器可以自由地再次移动东西。你想在这里完成什么?您是否希

这会打破吗?它编译得很好,但基于读数,我不确定它是否保证_ptRef始终指向构造函数中引用的结构

我猜“break”的意思是…GC会移动指针(_ptRef)指向的结构吗

情况是

-PointType0是一个结构

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

-仅当搜索操作找到相关的PointType0时,才会返回列表,并且此列表会被传递并对很多批次进行操作。

这不安全


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

是的……我很害怕。但谢谢你的明确和简洁的回答。
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) }