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?

5
  • 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 Commented Apr 6, 2019 at 16:02
  • It probably doesn't work. I was just trying to illustrate what I would like to achieve. @Ayxan Commented Apr 6, 2019 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. Commented Apr 6, 2019 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. Commented Apr 6, 2019 at 16:10
  • I'd keep things simple if I were you, and go with @Ayxan s suggestion of using std::vector. Commented Apr 6, 2019 at 16:11

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

3 Comments

The thing is whenever you need a variable array of some sort in C++, the answer is always std::vector
@George Vectors have very similar performance 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
@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() 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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.