1

So if I have a class with a 2D array that I want to initialize with two parameters passed into the constructor, how would I do that, I keep running into errors because it won't let me update the two-d array at all in the constructor.

-- Update from the comments:

In my header file I tried both

int array[][] 

and

int **array 

and then in the .cpp file in the constructor I'm trying to do

array = new int[arg1][arg2]

Neither declaration of the array in the header file worked.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
user1855952
  • 1,515
  • 5
  • 26
  • 55

3 Answers3

4

in the constructor I'm trying to do array = new array[arg1][arg2]

You need to specify the array type, like

array = new int[arg1][arg2];

Note that this works in C++11 only - when using older standards, the second array size needs to be const (which is probably not what you want).

There are also some additional articles discussing the same issue:

Ideally, since you are using C++ anyway, you should use std::vector as proposed in another answer.

Vectors use a lot of overhead though, don't they? I'm trying to keep my memory use light. –

Start with std::vector. Once your application is running properly from a functional perspective, if you are still concerned about memory usage and/or performance, do benchmarking. If you properly encapsulate your 2D array in a class, you can always change the actual implementation of the array with no impact on the code which uses it.


Technically, if you want to make sure that you have one flat memory area which contains your array, you could use a 1-dimensional array to simulate a 2-dimensional array, like in the following code (just to get you the idea, certainly needs some improvement, especially copy construction and assignment operators are missing):

class Array2D {
private:
    int *array;
    int size1;

public:
    Array2D(int arg1, int arg2) {
        size1 = arg1;
        array = new int[arg1 * arg2];
    }

    ~Array2D() {
        delete[] array;
    }

    int& at(int i1, int i2) {
        return array[i1 * size1 + i2];
    }
};

int main() {
    Array2D array(10, 10);

    array.at(2, 2) = 42;
    std::cerr << array.at(2, 2);

    return 0;
}
Community
  • 1
  • 1
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • That's not the issue, if you look at my updated comment, I fixed that. – user1855952 Jun 07 '13 at 07:47
  • 1
    Then, again, what error do you get? Note that the code in my answer [works in C++11 only](http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new) - otherwise, the second array size needs to be const – Andreas Fester Jun 07 '13 at 07:52
  • @Andreas maybe you should put your comment in the answer. It is important information (I was just writing a comment about this) – juanchopanza Jun 07 '13 at 07:58
  • Nice solution with `Array2D`, but it needs either to implement copy construction and copy assignment, or disallow them. – juanchopanza Jun 07 '13 at 08:22
  • Why is the line ```size1 = arg1;``` needed? – Angelos Jul 02 '20 at 18:18
0

Simplest solution would be:

std::vector<std::vector<VALUE>> arr2(X, std::vector<VALUE>(Y));
Enigma
  • 1,699
  • 10
  • 14
  • Vectors use a lot of overhead though, don't they? I'm trying to keep my memory use light. – user1855952 Jun 07 '13 at 07:48
  • @user1855952 I think the best solution is to use a single vector, and be clever about indices. Ideally, you could write a 2D array class using the vector as data storage. – juanchopanza Jun 07 '13 at 07:51
  • no, they don't. Especially when the space is reserved beforehand; as is happening in above example. – Enigma Jun 07 '13 at 07:51
  • @user1855952 http://stackoverflow.com/questions/381621/using-arrays-or-stdvectors-in-c-whats-the-performance-gap – Enigma Jun 07 '13 at 07:54
  • 1
    Well, it depends on the use case. If the number of elements is small, a vector of vectors could have a significant size overhead over, say, a plain 2D array of contiguous elements. But one would first have to make sure this is actually an issue. The best approach is to abstract the storage away inside a 2D array class. – juanchopanza Jun 07 '13 at 07:55
0

Here is an 2d array example with bounds check and custom type, based upon the example from Andreas Fester.

#include <stdexcept>

template <typename T>
class Array2D {
private:
    T *array;
    unsigned int sizeX;
    unsigned int sizeY;

public:
    Array2D(unsigned int X, unsigned int Y) {
        sizeX = X;
        sizeY = Y;
        array = new T[X * Y];
    }

    ~Array2D() {
    delete[] array;
    }

    T& at(unsigned int X, unsigned int Y) {
        if((X > sizeX) || (Y > sizeY))
            throw std::out_of_range("Bla bla");

        return array[X * sizeX + Y];
    }
};

int main() {
    double MyValue;
    Array2D<double> *MyArray = new Array2D<double>(10, 100);
    MyArray->at(1,1) = 10.1;
    MyValue = MyArray->at(1,1);
    printf("Array value = %3.3f\n", MyValue);

    return 0;
}