我有这段代码:
GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned);
try
{
IntPtr pointer = handle.AddrOfPinnedObject();
}
finally
{
if (handle.IsAllocated)
{
handle.Free();
}
}
然后有这个:
unsafe
{
fixed (int* pArray = array)
{
IntPtr intPtr = new IntPtr((void *) pArray);
}
}
什么是更快,更少的内存消耗?请问咨询方法是什么?
附加:
我从C#调用OpenCV DLL ...
public byte[] ToJpegData(int qua)
{
byte[] data = null;
int[] p = new int[3];
p[0] = 1;
p[1] = qua;// desired_quality_value;
p[2] = 0;
GCHandle handle = GCHandle.Alloc(p, GCHandleType.Pinned);
try
{
IntPtr quality = handle.AddrOfPinnedObject();
//IntPtr quality = new IntPtr(p);
IntPtr mat = CvInvoke.cvEncodeImage(".jpg", Ptr, quality);// IntPtr.Zero);
MCvMat cvMat = (MCvMat)Marshal.PtrToStructure(mat, typeof(MCvMat));
data = new byte[cvMat.rows * cvMat.cols];
Marshal.Copy(cvMat.data, data, 0, data.Length);
CvInvoke.cvReleaseMat(ref mat);
}
finally
{
if (handle.IsAllocated)
{
handle.Free();
}
}
return data;
}