I have following piece of C++ code that needs to be C Sharped.
int* pData = new int[128];
for(int i = 0; i < 128; i++)
{ pData[i] = i*2 ;}
This pData int* is later passed to a function as void*
Now I need to put all this in C#. What I have done is as follows,
Int32[] tempData = new Int32[128];
for(int i = 0; i < 128; i++)
{ tempData[i] = i*2 ;}
int size = Marshal.SizeOf(tempData[0]) * tempData.Length;
IntPtr ptrData = Marshal.AllocHGlobal(size);
Marshal.Copy(tempData, 0, ptrData, tempData.Length);
Later I pass the ptrData to the C# function.But I get the run time error : Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Thanks in Advance.
Should do the trick…