1

I want to first initialize "dummy" array when i create a object. I have this in private part of my class.

// Initialize dummy array when object is initialized.
int* matrix_[0][0];

Then i want later to initialize new array to replace the dummy one (one that has actual size). I have method for this in my class:

void set_map_size(int width, int height) {
    int* pm[width][height];
    matrix_ = pm;
}

So the problem is when I try to initialize this array after the creation of the object. I want other methods / other objects to have access to this array.

Maybe I should have pointer? Initialize pointer as nullptr when object is created and then change pointer to point to array?

Christophe
  • 68,716
  • 7
  • 72
  • 138
Tuki
  • 143
  • 1
  • 3
  • `int* matrix_[0][0];` doesn't look right, is this standard? Shouldn't you use `malloc` or `new` instead of a local automatic variable in that function? To avoid all these, consider using `std::vector` if you can – Aykhan Hagverdili Apr 06 '19 at 16:02
  • It probably doesn't work. I was just trying to illustrate what I would like to achieve. @Ayxan – Tuki Apr 06 '19 at 16:07
  • @Ayxan `int* matrix_[0][0]` isn't right, it declares a 0 length 2d array of pointers. But malloc should pretty much never be used in c++, and raw new only very rarely has a place. – George Apr 06 '19 at 16:08
  • I would like to have there something has place holder so that i could replace it later with actual 2d array when i know the size. At the initialization of the object I don't yet know the size of the array I want to use. – Tuki Apr 06 '19 at 16:10
  • I'd keep things simple if I were you, and go with @Ayxan s suggestion of using std::vector. – George Apr 06 '19 at 16:11

1 Answers1

1

The right way to do this in C++ is to use vectors.

vector<vector<int>> matrix_;  

You may then dynamically resize your matrix

void set_map_size(int width, int height) {
    matrix_.resize(height); 
    for (auto &x : matrix_) 
       x.resize(width);
}

Because arrays are of fixed size and variable length array are not standard C++ (even if some compilers accept it).

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • The thing is whenever you need a variable array of some sort in C++, the answer is always `std::vector` – Aykhan Hagverdili Apr 06 '19 at 16:12
  • 2
    @George [Vectors have very similar performance](https://stackoverflow.com/a/381656/10147399) and handling a dynamically allocated array is enough of a pain that unless you absolutely need that infinitesimal performance gain in your high-performance library code, you should go with `std::vector` – Aykhan Hagverdili Apr 06 '19 at 16:22
  • @George If performance is at stake, the 2D vector should be flattened to a 1D vector. This could make a significant difference. If it would nevertheless appear that there would be a performance issue, you could still use a pointer to [`data()`](https://en.cppreference.com/w/cpp/container/vector/data) in the time critical section. But do this only if the performance issue was really confirmed by measurement, because "*premature optimization is the root of all evil*" – Christophe Apr 06 '19 at 16:33