views:

140

answers:

5

Hi,

I need to pass a double [][6] to a method. But I don't know how to create that two-dimensional array.

6 is a fixed size (or a "literal constant", if my terminology is right), which the method accepts. I was trying something like this, but without success...

double *data[6] = new double[6][myVariableSize];

So, the method really looks like:

int myMethod(double obj_data[][6]);

Thanks in advance

+1  A: 

Your method definition should look like this (to match your definition):

int myMethod(double obj_data[6][]);

..but that's not valid C++ because only the first dimension can be undefined. Try the following:

int myMethod(double **obj_data, const int numOfColumns, int numOfRows)
{
    // Set the element in the last column / row to 5
    obj_data[numOfRows-1][numOfColumns-1] = 5;

    return 0;
}


int main(int argc, char* argv[])
{
    // Define array size
    int myNumOfRows = 5;
    const int numOfColumns = 6;

    // Allocate memory
    double** data = new double*[myNumOfRows];
    for (int i = 0; i < myNumOfRows; ++i)
    {
        data[i] = new double[numOfColumns];
    }

    // Do something with the array
    myMethod(data, numOfColumns, myNumOfRows);

    return 0;
}
Jon Cage
It's not obvious which way round is correct, but the OP does seem to be getting the two dimensions mixed up
finnw
+3  A: 

In C++ you could use std::array<std::vector<double>, 6>.

typedef std::array<std::vector<double>, 6> my_array_t;

int myMethod( const my_array_t& obj_data );

If your compiler still doesn't support std::array you could use boost::array instead.

Kirill V. Lyadvinsky
`s/c++/c++0x/g`....
Luther Blissett
Do you mean C++0x?
Mike Seymour
`boost::array` is available in C++. `std::array` is from C++0x, but already has supported by many modern compilers.
Kirill V. Lyadvinsky
It's not a double[6][], it's a double[][6]. That is, a std::vector<std::array<double, 6>>.
DeadMG
@DeadMG, depending on what OP really wants. It is not clear from the question.
Kirill V. Lyadvinsky
+2  A: 

I cannot tell from the question which dimension is which, but this might be worth a try:

double (*data)[6] = new double[myVariableSize][6];
finnw
Sorry, I just put the error in your body for the monospaced font.
Martijn Courteaux
@Martijn Courteaux, `std::bad_alloc` usually means you are out of memory or trying to allocate an array of negative length. What was the value of `myVariableSize`? Are you sure it is initialized?
finnw
To get monospaced text in a comment surround it with backticks (`)
finnw
AHA!! Thanks for the hint! The size was negative!
Martijn Courteaux
+3  A: 

This should work:

int myMethod(double obj_data[][6])
...
int myVariableSize = 10;
double (*data)[6] = new double[myVariableSize][6];
myMethod(data);

As mentioned only the first dimension can be variable!

kaptnole
+1: This works. I tried the same thing originally but couldn't get it to compile for some reason.
Jon Cage
A: 

My colleague and I could get no variations of the problem you proposed to work. But this does work and does compile:

int myMethod(double** obj_data)
{
    return 5;
}

int _tmain(int argc, _TCHAR* argv[])
{
    const int myVariableSize = 3;
    double** b = new double*[3];
    for (int i = 0 ; i < 3;i++)
    {
        b[i] = new double[myVariableSize];
    }

    myMethod(data);

    return 0;
}

I propose you just go back to using a double vector for a 2D array, or use some of Boost's (so I'm told) multi-dimensional arrays.

C Johnson