Pointers to pointers seem to be the only way to dynamically declare a two dimensional (or multidimensional) array besides doing the leg work myself and coding the pointer arithmetic to create a "fake" multidimensional array out of a one dimensional array. I understand this is the way it is but why?!
-
Have a look at "array accessors" and "pointer arithmetic". – Jean-Baptiste Yunès Jul 05 '16 at 09:14
-
1You can use things like `vector
`, but unless your 2D array is not a rectangle you are arguably better off using a single vector and doing the leg work. – nwp Jul 05 '16 at 09:15 -
1It's hard to answer your question because it's hard to understand what you think the other option would be. Magic? Someone has to do the hard work, right? You can certainly use whatever libraries you like best, right? – David Schwartz Jul 05 '16 at 09:16
-
why do you call it "fake"? Also on paper you can write a 2D array in a 2d representation, but anything beyond that would be what you call "fake". Memory is 1D, thus any higher dimensional data structure has to do some kind of "fake" – 463035818_is_not_an_ai Jul 05 '16 at 09:18
-
1Both dimensions need to be hard-coded (well, they need to be constant expressions). You're probably confusing a 2D array with a pointer to a 1D array. – juanchopanza Jul 05 '16 at 09:26
-
A pointer is the generic way to access dynamic resources. Therefore, if you want to have a "real" dynamic 2D structure you need pointer to pointer (or something that wraps just that behaviour like `vector
>`). If your 2D data is dense you should absolutely consider to use a 1D data layout and use a "fake" multidimensional array. [See the answers to this question](http://stackoverflow.com/questions/17259877/1d-or-2d-array-whats-faster/17260533) why you'd want to do that. – Pixelchemist Jul 05 '16 at 09:51 -
Thanks for the reply @Pixelchemist the 1D vs 2D question was what I was trying to ask, sorry for confusion caused. I am very new to C++ – Harry de winton Jul 05 '16 at 10:13
-
@Harrydewinton: You're welcome. Note that the linked question is only about dynamically allocated 2D vs 1D arrays/matrices. A hardcoded array like `T a[5][5]` has a contigous memory layout (unlike the `T**` dynamic 2D case). – Pixelchemist Jul 05 '16 at 10:17
-
@tobi303: Mercury delay line memory was 1D, but since we moved to core memory we've been using 2D memory. And the very latest development is to move to 3D stacked memory. The 1D is faked ;) – MSalters Jul 05 '16 at 11:02
-
@MSalters well, then I was talking nonsense ;). However, my point was just that any dimensionality can be flattened into a lower dimensional structure and I wouldnt call it "fake". – 463035818_is_not_an_ai Jul 05 '16 at 11:13
-
@tobi303: I wasn't entirely serious, the memory may be physically 2D but the logical representation is 1D. The transform between 1D and a 2D array is trivial in both directions, given a hardcoded width (which is the whole point of this question). – MSalters Jul 05 '16 at 11:27
-
@MSalters dont worry, I didnt take it too seriously :P – 463035818_is_not_an_ai Jul 05 '16 at 11:29
2 Answers
First of all, a two dimensional array is not the same as a pointer to a pointer. While the latter is a pointer to an array of pointers (to arrays), the former looks like this in memory:
char v[2][3] = {{1,3,5},{5,10,2}};
Content: | 1 | 3 | 5 | 5 | 10 | 2
Address: v v+1 v+2 v+3 v+4 v+5
To access v[x][y]
, the compiler rewrites it as: *(v + y * WIDTH + x)
So as you can see, WIDTH (the second dimension specified) is needed to do the calculation.
You can omit the first dimension in function parameters since they are adjusted to pointers:
int f (int v[3][10])
is the same as
int f (int v[][10])
and the same as
int f (int (*v)[100])
(Since arrays are adjusted to pointers in function parameters) but not the same as
int f (int *v[100])
(Since this is an array[100] (that is adjusted to a pointer) of pointers)
Anyway I suggest you to use std::vector<std::vector<Type>>
if you need a two-dimensional array.

- 1,720
- 11
- 21
Why is a hard coded width required when declaring a two dimensional array?
C++ arrays require their dimensions to be constant expressions. For 2D arrays (really arrays of arrays), this applies to both dimensions. There is no exception for the "width" or the "height".

- 223,364
- 34
- 402
- 480