C# 互操作在不封送的情况下传递大型二维数组 我有一个巨大的矩形数组(2GB+),我想传递给C++ DLL。我希望在托管代码中分配内存一次,而不是封送它(太大)。数据是在循环中顺序填充的二维数组。我可以使用以下托管阵列创建它: float[,] masterMatrix = new float[dim1, dim2]; // 2GB + foreach(Sequence seq in sequences) { // get the next matrix & calculate its byte size float[,] result = seq.Process(); int byteSize = (result.GetLength(0) * result.GetLength(1)) * sizeof(float); // append it to the master matrix Buffer.BlockCopy(result, 0, masterMatrix, insertByteIndex, byteSize); insertByteIndex += byteSize; }
但是如果我使用IntPtr(这样我就可以直接将它传递到.dll),我如何将C# 互操作在不封送的情况下传递大型二维数组 我有一个巨大的矩形数组(2GB+),我想传递给C++ DLL。我希望在托管代码中分配内存一次,而不是封送它(太大)。数据是在循环中顺序填充的二维数组。我可以使用以下托管阵列创建它: float[,] masterMatrix = new float[dim1, dim2]; // 2GB + foreach(Sequence seq in sequences) { // get the next matrix & calculate its byte size float[,] result = seq.Process(); int byteSize = (result.GetLength(0) * result.GetLength(1)) * sizeof(float); // append it to the master matrix Buffer.BlockCopy(result, 0, masterMatrix, insertByteIndex, byteSize); insertByteIndex += byteSize; },c#,c++,arrays,interop,C#,C++,Arrays,Interop,但是如果我使用IntPtr(这样我就可以直接将它传递到.dll),我如何将结果的数据复制到非托管内存中?Copy只处理一维数组。我知道我可以一个元素一个元素地做,但我希望有更好/更快的方法 IntPtr pointer = Marshal.AllocHGlobal(maxNumBytes); // 2GB + foreach(Sequence seq in sequences) { // get the next matrix & calculate its byte siz
结果的数据复制到非托管内存中?Copy只处理一维数组。我知道我可以一个元素一个元素地做,但我希望有更好/更快的方法
IntPtr pointer = Marshal.AllocHGlobal(maxNumBytes); // 2GB +
foreach(Sequence seq in sequences)
{
// get the next matrix & calculate its byte size
float[,] result = seq.Process();
int byteSize = (result.GetLength(0) * result.GetLength(1)) * sizeof(float);
// append it to the master matrix
Marshal.Copy(/* can't use my result[,] here! */, 0, pointer, byteSize);
insertByteIndex += byteSize;
}
谢谢你的建议。有什么建议吗?请勿使用2Gb阵列:D