-1
PInkove部分取自一些SO回答(对不起,我失去了原来的链接)。来自PInvoke的memcmp在C#中对于大于4x4的数组无法正常工作
以下是完整的程序。输出是false
。
using System;
using System.Runtime.InteropServices;
namespace Memcpy
{
class Program
{
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int memcmp(Array b1, Array b2, long count);
public static bool CompareArrays(Array b1, Array b2)
{
// Validate buffers are the same length.
// This also ensures that the count does not exceed the length of either buffer.
return b1.Length == b2.Length && memcmp(b1, b2, b1.Length) == 0;
}
static void Main(string[] args)
{
var array1 = new int[,]
{
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
};
var array2 = new int[,]
{
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
};
Console.WriteLine(CompareArrays(array1, array2));
}
}
}
如果我改变阵列的大小在4x4输出变为true
为什么memcmp这样的行为?
你认为b1.Length的价值是什么? – pm100
'CompareArrays'里面的长度都是相等的,memcmp的结果不是= 0。 只需设置一个断点: 'b1.Length = 20' 'b2.Length = 20' –
'memcmp'需要两个空指针和以字节为单位的长度。是什么让你认为数组的'Length'属性是以字节为单位的长度?不是,它是数组中元素的数量。另外,你可能想调用'memcmp',因为你认为这是一个有效的函数?它是,但生成的编组代码可能不是!事实上,我认为编组代码可能是这里的罪魁祸首。 –