0

I am trying to do the following:

class Test{
private:
    int x;
    int y;
    // Create array[][] here
public:
    Test(const int x, const int y){
        this->x = x;
        this->y = y;
        // set: array[x][y] here
    }
};

As you see I would like to create a 2d-Array, while the bounds will be given in the constructor. How can I achieve this?

It works with an usual array:

class Test{
private:
    int x;
    int y;
    int *array;
public:
    Test(const int x, const int y){
        this->array = new int[x]; // works
        // this->array = new int[x][y] does not work
        this->x = x;
        this->y = y;
    }
};
mbschenkel
  • 1,865
  • 1
  • 18
  • 40
makomako
  • 3
  • 1
  • See this post: http://stackoverflow.com/questions/8294102/proper-way-to-declare-a-variable-length-two-dimensional-array-in-c – Eric Palace Dec 23 '14 at 15:25
  • 4
    use an `std::vector` instead of raw pointers – vsoftco Dec 23 '14 at 15:26
  • Can you use templates? Your example will leak if you forget to add a destructor freeing the array. – Kimi Dec 23 '14 at 15:49
  • 1
    It might be worth considering using a single std::vector instead of a std:vector. Also, depending on the requirements, it might be a good idea to actually create the array in the initialization list instead of the body of the constructor: `Test(const unsigned x, const unsigned y) : x_(x), y_(y), buffer_(x*y) {}` will immediately provide a valid array with default values (as opposed to uninitialized values). The arithmetic to access the elements should be straightforward enough. – Come Raczy Dec 23 '14 at 17:29
  • possible duplicate of [The correct way to initialize a dynamic pointer to a multidimensional array?](http://stackoverflow.com/questions/18273370/the-correct-way-to-initialize-a-dynamic-pointer-to-a-multidimensional-array) – mbschenkel Dec 23 '14 at 17:35
  • @mbschenkel: Wrong duplicate. That one requires that trailing dimensions are known at compile-time. – Ben Voigt Dec 25 '14 at 20:56
  • @BenVoigt: You're right, the question is different. However, the accepted answer there would also apply here... That's probably what made me link to it. – mbschenkel Dec 26 '14 at 11:35

2 Answers2

0

you should consider allocating memory with pointers, should be as follows:

class Test{
private:
    int x;
    int y;
    int **array;
public:
    Test(const int x, const int y){
        int i;
        this->array = new int*[x]; // first level pointer asignation
        for(i=0;i<x;i++){
            this->array[x] = new int[y]; // second level pointer asignation
        }
        // this->array = new int[x][y] does not work
        this->x = x;
        this->y = y;
    }
};

see this.

Community
  • 1
  • 1
ogranada
  • 121
  • 1
  • 2
0

You may be bumping into the problem of "All dimensions must be constants except the leftmost" discussed here.

Instead, try the following:

class Test {
private:
        int x;
        int y;
        int** myArray;
public:

        Test(const int x, const int y) : x(x), y(y) {
                myArray = new int*[x];

                for (int firstDimension = 0; firstDimension < x; firstDimension++) {

                        myArray[firstDimension] = new int[y];

                        for (int secondDimension = 0; secondDimension < y; secondDimension++) {
                                myArray[firstDimension][secondDimension] = secondDimension;
                        }
                }
        }

        ~Test() {
                for(int i = 0; i < x; i++)
                        delete[] myArray[i];
                delete[] myArray;
        }
};
Community
  • 1
  • 1
jmjatlanta
  • 58
  • 7