简体   繁体   English

C#中的锯齿状数组固定

[英]Jagged array pinning in c#

I have kind of an issue. 我有一个问题。 I am trying to pin a jagged array (which i am using due to the sheer size of the data i am handling): 我正在尝试固定一个锯齿状的数组(由于我要处理的数据量太大,我正在使用该数组):

    public void ExampleCode(double[][] variables) {
        int nbObservants = variables.Length;

        var allHandles = new List<GCHandle>();
        double*[] observationsPointersTable = new double*[nbObservants];
        double** observationsPointer;
        GCHandle handle;

        for (int i = 0; i < nbObservants; i++) {
            handle = GCHandle.Alloc(variables[i], GCHandleType.Pinned);
            allHandles.Add(handle);
            observationsPointersTable[i] = (double*) handle.AddrOfPinnedObject(); // no prob here
        }

        fixed(double** obsPtr = observationsPointersTable) { // works just fine
            Console.WriteLine("haha {0}", obsPtr[0][0]);
        }

        handle = GCHandle.Alloc(observationsPointersTable, GCHandleType.Pinned); // won't work
        allHandles.Add(handle);
        observationsPointer = (double**) handle.AddrOfPinnedObject();

        // ...

        foreach (var aHandle in allHandles) {
            aHandle.Free();
        }
        allHandles.Clear();
    }

I need to use these double** in multiple parts of my code, and don't really want to explicitly pin them every time I need to use them. 我需要在代码的多个部分中使用这些double **,并且真的不想在每次使用它们时都明确地固定它们。 It seems to me that, as I can fix them through the usual fixed statement, I should be able to allocate a pinned handle to them. 在我看来,当我可以通过常规的fixed语句修复它们时,我应该能够为它们分配固定的句柄。

Is there any way to actually pin a double*[] ? 有什么方法可以实际固定double * []吗?

Apparently there is no way to do this. 显然没有办法做到这一点。 I therefore settled for using fixed statements instead of allocating pinned handles. 因此,我决定使用固定语句而不是分配固定的句柄。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM