我有一个巨大的矩形数组(2GB +),我想传递给C ++ DLL。我想在托管代码中分配一次内存,而不是编组(太大)。数据是一个2D数组,在循环中按顺序填充。我可以使用托管数组创建它,如下所示:
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),如何将result
的数据复制到非托管内存中? Marshal.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;
}
感谢任何想法。