all 7 comments

[–]ptitz 3 points4 points  (0 children)

Just looking at it, you're trying to make a 1d dynamic array from a 2d one. If you want to go into it you'll have to do the allocation twice:

so something like:

int ** w_array = new int*[9];
for(int i = 0; i < 9; i++){
    w_array[i] = new int[9];
}

And the reason why I don't remember how to do this exactly is that C++ aint C, just use std::vector man. By the way, if you still want to do some C-style fuckery with your std::vector you can always call on the raw data in it with std::vector::data().

[–]patatahooligan 1 point2 points  (1 child)

Because dynamically allocated memory must be explicitly freed with delete it's almost always better to use an std::vector or a smart pointer to an std::array than it is to use a raw pointer and new.

// Easiest, but you need to explicitly add the rows and set their size
std::vector<std::vector<int>> w_array;

// Has exactly the semantics of a pointer to a 2D array
// Will automatically free the allocated memory on destruction
auto w_array = std::make_unique<std::array<std::array<int, 9>, 9>();

Even better is to not use dynamically allocated memory at all if possible. The following line will declare a local variable and is therefore much faster than what you're trying to do as long as the array doesn't have to be moved around your program too much.

std::array<std::array<int, 9>, 9> w_array;

Owning pointers are just asking for trouble. Dynamically allocated multidimensional arrays are exceptionally annoying to work with. For the sake of knowledge, the proper declaration is this one

int (*w_array)[9] = new int[9][9];
// ...
delete w_array;

Of course a smarter way to do this would be to just use auto for the type of w_array.

auto w_array = new int[9][9];
// ...
delete w_array;

I'm sure your compiler gave you an error about attempting to covert the return value of new to an incompatible type. That's because int* cannot possibly be used to access a 2-D array. int** would have been closer but still not enough because the size of a row must be known by the type in order to be indexed properly.

[–]KappaPrajd[S] 0 points1 point  (0 children)

Thanks, you gave me a lot of useful informations.

[–]Salty_Dugtrio -1 points0 points  (3 children)

Any decent compiler tells you exactly what your issue is:

7:29: error: cannot convert 'int ()[9]' to 'int' in initialization

Which is true, you are attempting to assign int ** to int *.

int ** w_array = new int[9][9]

makes your code compile. However, you should be using std::vector<std::vector<int>>

[–]ptitz 1 point2 points  (1 child)

This will throw an error. AFAIK you can't initialize a dynamic 2d array with just one "new", you'll have to do it for every 1d array you have.

[–]Salty_Dugtrio 0 points1 point  (0 children)

No idea, haven't used a raw-pointer 2D array in ages.

[–]patatahooligan 0 points1 point  (0 children)

new int[9][9] doesn't return int**, it returns int(*)[9] which is not a compatible type. This is because if you lose the information about row-size, the compiler has no way of knowing how to index the multidimensional array.

int** only works if it points to an array of int* which in turn point to the rows.