Committing an array of an array in C # (insecure code)

I am trying to find a solution on how to pass an array of arrays from C # to a native function. I already have a function delegate (Marshal.GetDelegateForFunctionPointer), but now I'm trying to pass a multidimensional array into it (or rather, an array of arrays).

This code example works when the input has 2 sub-arrays, but I need to be able to handle any number of sub-arrays. What is the easiest way you can do this? I would prefer not to copy data between arrays, as this will happen in a real-time loop (I communicate with the audio effect)

public void process(float[][] input)
{
    unsafe
    {
        // If I know how many sub-arrays I have I can just fix them like this... but I need to handle n-many arrays
        fixed (float* inp0 = input[0], inp1 = input[1] )
        {
            // Create the pointer array and put the pointers to input[0] and input[1] into it
            float*[] inputArray = new float*[2];
            inputArray[0] = inp0;
            inputArray[1] = inp1;
            fixed(float** inputPtr = inputArray)
            {
                // C function signature is someFuction(float** input, int numberOfChannels, int length)
                functionDelegate(inputPtr, 2, input[0].length);
            }
        }
    }
}
+5
source share
3 answers

.

, , , , . .

, , .

, , . .

0

- . , , . . , array[i][j] = oneDimArray[i *n + j], n . , :

public void process(float[] oneDimInput, int numberOfColumns)
{
    unsafe
    {
        fixed (float* inputPtr = &oneDimInput[0])
        {
                // C function signature is someFuction(
                // float* input, 
                // int number of columns in oneDimInput
                // int numberOfChannels, 
                // int length)
                functionDelegate(inputPtr, numberOfColumns, 2, oneDimInput[0].length);
        }
    }
}

I also need to note that two arrays of measurements are rarely used in high-performance computing libraries such as Intel MKL, Intel IPP, and many others. Even the BLAS and Lapack interfaces contain only one dimensional array and emulate two dimensions using the aproach I mentioned (for performance reasons).

0
source

All Articles