Skip to content

Instantly share code, notes, and snippets.

IntPtr pdata = Marshal.AllocHGlobal(Data.Length * sizeof(int));
int* data = (int*)pdata;
try
{
Marshal.Copy(Data, 0, pdata, Data.Length);
for (int i = 0; i < Data.Length - (BUFFER_SIZE + 1); i += BUFFER_SIZE)
{
int* sub = data + i;
if (sub[BUFFER_SIZE - 1] != i + BUFFER_SIZE - 1)
fixed (int* data = &Data[0])
{
for (int i = 0; i < Data.Length - (BUFFER_SIZE + 1); i += BUFFER_SIZE)
{
int* sub = data + i;
if (sub[BUFFER_SIZE - 1] != i + BUFFER_SIZE - 1)
throw new Exception();
}
}
var spanData = new Span<int>(Data);
for (int i = 0; i < Data.Length - (BUFFER_SIZE + 1); i += BUFFER_SIZE)
{
var subSpan = spanData.Slice(i, BUFFER_SIZE);
if (subSpan[BUFFER_SIZE - 1] != i + BUFFER_SIZE - 1)
throw new Exception();
}
var subArray = new int[BUFFER_SIZE];
for (int i = 0; i < Data.Length - (BUFFER_SIZE + 1); i += BUFFER_SIZE)
{
Array.Copy(Data, i, subArray, 0, BUFFER_SIZE);
if (subArray[subArray.Length - 1] != i + BUFFER_SIZE - 1)
throw new Exception(); // avoid optimization
}