Reputation: 11
Have been reading this is an "unsafe" code and "IntPtr" is normally not to function this way.
Can someone suggest an alternative or a solution.
My skills are limited in C#. Thanks for any help !!
for (num4 = 1; num4 < i; num4 += 2)
{
for (num = num4; num <= length; num += num5)
{
num2 = num + i;
num11 = (num7 * numRef[(num2 - 1) * 8]) - (num8 * numRef[num2 * 8]);
double num10 = (num7 * numRef[num2 * 8]) + (num8 * numRef[(num2 - 1) * 8]);
numRef[(num2 - 1) * 8] = numRef[(num - 1) * 8] - num11;
numRef[num2 * 8] = numRef[num * 8] - num10;
IntPtr ptr1 = (IntPtr)(numRef + ((num - 1) * 8));
//ptr1[0] += (IntPtr) num11;
ptr1[0] += (IntPtr)num11;
IntPtr ptr2 = (IntPtr)(numRef + (num * 8));
//ptr2[0] += (IntPtr) num10;
ptr2[0] += (IntPtr)num10;
}
num7 = (((num9 = num7) * num13) - (num8 * num14)) + num7;
num8 = ((num8 * num13) + (num9 * num14)) + num8;
}
Upvotes: 1
Views: 1250
Reputation: 21561
If you want to use pointer arithmetic in C# you will need to use the unsafe and fixed keyword, as follows:
public unsafe void Foo()
{
int[] managedArray = new int[100000];
// Pin the pointer on the managed heap
fixed (int * numRef = managedArray)
{
for (num4 = 1; num4 < i; num4 += 2)
{
for (num = num4; num <= length; num += num5)
{
num2 = num + i;
num11 = (num7 * numRef[(num2 - 1) * 8]) - (num8 * numRef[num2 * 8]);
double num10 = (num7 * numRef[num2 * 8]) + (num8 * numRef[(num2 - 1) * 8]);
// You can now index the pointer
// and use pointer arithmetic
numRef[(num2 - 1) * 8] = numRef[(num - 1) * 8] - num11;
numRef[num2 * 8] = numRef[num * 8] - num10;
...
int * offsetPtr = numRef + 100;
// Equivalent to setting numRef[105] = 5;
offsetPtr[i+5] = 5;
}
num7 = (((num9 = num7) * num13) - (num8 * num14)) + num7;
num8 = ((num8 * num13) + (num9 * num14)) + num8;
}
}
}
Upvotes: 2