Return Byte[] : reference or value?

  • Thread starter Thread starter Drew
  • Start date Start date
D

Drew

Does this return the array by value or reference? Because of DotNet's
memory handling, I don't know how I could figure this out... Is there
a way?

System::Byte Converter::ToByteArray(vector<XTBYTE>& data)[]
{
XTSIZE_T tSize = data.size();

System::Byte aData[] = new System::Byte[tSize]; // Managed
destination array
System::Byte __pin* pDest = &aData[0]; // Pin the
destination array

memcpy(pDest, &data[0], tSize);

return (aData);
}
 
Drew said:
Does this return the array by value or reference? Because of DotNet's
memory handling, I don't know how I could figure this out... Is there
a way?

System::Byte Converter::ToByteArray(vector<XTBYTE>& data)[]
{
XTSIZE_T tSize = data.size();

System::Byte aData[] = new System::Byte[tSize]; // Managed
destination array
System::Byte __pin* pDest = &aData[0]; // Pin the
destination array

memcpy(pDest, &data[0], tSize);

return (aData);
}

The variable "aData" points to a managed array. Managed arrays are reference
types, so the function can only return a reference to it. You can't copy
reference types in the normal C++ way, and you can't pass or return them by
value; it's always by reference.

P.S. Besides the __pin/memcpy approach, look at Marshal.Copy:

http://msdn.microsoft.com/library/d...ntimeinteropservicesmarshalclasscopytopic.asp
 
Back
Top