all groups > dotnet interop > february 2006 >
You're in the

dotnet interop

group:

Looping memory array in C#


Looping memory array in C# Totto
2/17/2006 5:56:42 PM
dotnet interop:
Hi all,
I'm calling a win32 function from C# that returns a far pointer to an array
of data.
The looping in C goes like this:

pSint8 = (char* )pData;
pSint16 = (Int16*)pData;
pSint32 = (Int32*)pData;
pFloat = (float*)pData;
for (j = 0 ; j < numb ; j++)
{
switch (DataType)
{
case dtSINT8:
fVal = (float)(*pSint8++);
break;
case dtSINT16:
fVal = (float)(*pSint16++);
break;
case dtSINT32:
fVal = (float)(*pSint32++);
break;
case dtFLOAT:
fVal = (*pFloat++);
break;
}
}

Is it possible to do this looping i C# without going to unsafe code?

Regards Totto



Re: Looping memory array in C# Mattias Sjögren
2/19/2006 12:22:11 AM

[quoted text, click to view]

Well you can use Marshal.Copy to bring the whole array into managed
memory at once and then iterate through that.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Re: Looping memory array in C# Totto
2/19/2006 4:49:06 PM
Thanks Mattias,
Do you know if there is a big performance penalty doing it this way opposed
to using unsafe code and pointer arithmetic?


Totto

[quoted text, click to view]

Re: Looping memory array in C# Mattias Sjögren
2/21/2006 11:30:47 PM
[quoted text, click to view]

I wouldn't think so but I suggest you measure it. Since you're making
a copy of the data, you'll of course double the amount of memory you
need.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
AddThis Social Bookmark Button