views:

182

answers:

1

Dear Sirs,

I'm trying to create a Managed Array of doubles from an array of bytes. I have the problem working currently, but I wanted to optimize. Here's some code that I would like to work:

private unsafe static double[] _Get_Doubles(byte[] _raw_data)
{
    double[] ret;
    fixed (byte* _pd = _raw_data)
    {
        double* _pret = (double*)_pd;
        ret = (double[])*_pret; //FAILURE
    }
}

Please let me know how to cope with these problems.

-Aaron

+1  A: 

One of the key things to notice about the code you have posted is that there is no way to know how many items are pointed to by the return value, and a managed array needs to know how big it is. You can return a double* or create a new double[XXX] and copy the values or even (if the count is constant) create a struct with a public fixed double _data[2]; member and cast the raw data to that type.

Dolphin
That is basically what I wound up doing. I created the double array with a fixed length, then copied my values in there one at a time. It would be nice to, in a C++ way, not allocate the new double array. Yeah, a struct with a double[] and a byte[] set to overlap like the al, ax, and eax registers, but the overhead of creating a struct might be more than what I'm doing...which might make you think, why on earth would you want to be so convoluted in your array-copying? To which I answer ` '.
Limited Atonement