Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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.

share|improve this question
4  
I keep running into errors - what does your code look like? What are the errors? – Andreas Fester Jun 7 '13 at 7:37
    
How is the array declared? – jrok Jun 7 '13 at 7:39
    
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. – user1855952 Jun 7 '13 at 7:40
1  
Don't confuse a pointer to pointer with a 2D array. – juanchopanza Jun 7 '13 at 7:44
up vote 3 down vote accepted

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;
}
share|improve this answer
    
That's not the issue, if you look at my updated comment, I fixed that. – user1855952 Jun 7 '13 at 7:47
1  
Then, again, what error do you get? Note that the code in my answer works in C++11 only - otherwise, the second array size needs to be const – Andreas Fester Jun 7 '13 at 7: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 7 '13 at 7:58
    
Nice solution with Array2D, but it needs either to implement copy construction and copy assignment, or disallow them. – juanchopanza Jun 7 '13 at 8:22
    
@juanchopanza Right :-) As I wrote, its only boilerplate code ... – Andreas Fester Jun 7 '13 at 8:23

Simplest solution would be:

std::vector<std::vector<VALUE>> arr2(X, std::vector<VALUE>(Y));
share|improve this answer
    
Vectors use a lot of overhead though, don't they? I'm trying to keep my memory use light. – user1855952 Jun 7 '13 at 7: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 7 '13 at 7:51
    
no, they don't. Especially when the space is reserved beforehand; as is happening in above example. – Enigma Jun 7 '13 at 7:51
    
@user1855952 stackoverflow.com/questions/381621/… – Enigma Jun 7 '13 at 7: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 7 '13 at 7:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.