views:

1418

answers:

4

This question builds off of a previously asked question: Pass by reference multidimensional array with known size

I have been trying to figure out how to get my functions to play nicely with 2d array references. A simplified version of my code is:

    unsigned int ** initialize_BMP_array(int height, int width)
    {
       unsigned int ** bmparray;
       bmparray = (unsigned int **)malloc(height * sizeof(unsigned int *));
       for (int i = 0; i < height; i++)
       {
     bmparray[i] = (unsigned int *)malloc(width * sizeof(unsigned int));
       }
      for(int i = 0; i < height; i++)
     for(int j = 0; j < width; j++)
     {
          bmparray[i][j] = 0;
     }
    return bmparray;
    }

I don't know how I can re-write this function so that it will work where I pass bmparray in as an empty unsigned int ** by reference so that I could allocate the space for the array in one function, and set the values in another.

+3  A: 
typedef array_type unsigned int **;
initialize_BMP_array(array_type& bmparray, int height, int width)
BigSandwich
+1  A: 

To use the safer and more modern C++ idiom, you should be using vectors rather than dynamically allocated arrays.

void initialize_BMP_array(vector<vector<unsigned int> > &bmparray, int height, int width);
Mark Ransom
+2  A: 

Mmm... maybe I don't understand well your question, but in C you can pass "by reference" by passing another pointer indirection level. That is, a pointer to the double pointer bmparray itself:

unsigned int ** initialize_BMP_array(int height, int width, unsigned int *** bmparray)
{
   /* Note the first asterisk */
   *bmparray = (unsigned int **)malloc(height * sizeof(unsigned int *));

   ...

   the rest is the same but with a level of indirection


   return *bmparray;
}

So the memory for the bmparray is reserved inside the function (and then, passed by reference).

Hope this helps.

Diego Sevilla
+3  A: 

Use a class to wrap it, then pass objects by reference

class BMP_array
{
public:
    BMP_array(int height, int width)
    : buffer(NULL)
    {
       buffer = (unsigned int **)malloc(height * sizeof(unsigned int *));
       for (int i = 0; i < height; i++)
       {
        buffer[i] = (unsigned int *)malloc(width * sizeof(unsigned int));
       }

    }

    ~BMP_array()
    {
        // TODO: free() each buffer
    }

    unsigned int ** data()
    {
        return buffer;
    }

private:
// TODO: Hide or implement copy constructor and operator=
unsigned int ** buffer
};
total
This seems like a lot of boiler plate just to pass an array. If he only ever works with the raw data, why wrap it in a class?
BigSandwich
It depends how many different operations are made on the data, but my opinion is that, even just for the initialize operation and the memory management, it's worth using a class.
total
I agree. There's no way I would allocate a resource without wrapping it somehow.
Rob K
Yeah, but that's not what the question was. The question was syntactical - how to pass an array. That's not the same as whether you should pass an array. And I can think of cases where you would want to pass a raw array.
BigSandwich