By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,830 Members | 812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,830 IT Pros & Developers. It's quick & easy.

Return Byte[] : reference or value?

P: n/a
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);
}
Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Drew wrote:
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/de...scopytopic.asp

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.