导入C++ DLL的问题

Ane*_*nee 0 c# c++ dllimport

我试图从我的程序中的C#代码访问此C++函数

Tridiagonal3 (float** mat, float* diag, float* subd)
{
   float a = mat[0][0], b = mat[0][1], c = mat[0][2],
                        d = mat[1][1], e = mat[1][2],
                                       f = mat[2][2];

}
Run Code Online (Sandbox Code Playgroud)

通话如下所示

tred2(tensor, eigenValues, eigenVectors);
Run Code Online (Sandbox Code Playgroud)

张量是指,float[,]而特征值和特征向量是float[]数组.

当我尝试这样做时,我得到一个例外

Access violation reading location 0x3f5dce99
Run Code Online (Sandbox Code Playgroud)

当我尝试访问

float a = mat[0][0]
Run Code Online (Sandbox Code Playgroud)

可能会发生什么?

use*_*016 5

Tridiagonal3 (float** mat, float* diag, float* subd)

mat是双指针类型(指向指针的指针).在C#中,float [,] 不是双指针.它只是用于访问多维数组的语法糖,就像你要做的mat[x + y * width]那样mat[y][x];

换句话说,您将传递float*给您的C++应用程序,而不是float**.

您应该mat使用手动偏移更改用于访问元素的方式,例如mat[y + 2 * x]