Return Byte[] : reference or value?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Drew

    #1

    Return Byte[] : reference or value?

    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::ToBy teArray(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);
    }
  • Doug Harrison [MVP]

    #2
    Re: Return Byte[] : reference or value?

    Drew wrote:
    [color=blue]
    >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::ToBy teArray(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);
    > }[/color]

    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++

    Comment

    Working...