In his book , The C ++ Programming Language, Bjarne Stroustrup mentions (C.7.2, p. 838 Special Edition, 2000):
... We can initialize ma as follows:
void int_ma() { for(int i=0; i<3; i++) for(int j=0; j<5; j++) ma[i][j] = 10 * i + j; }
...
The ma array is just 15 int that we are accessing, as if it were 3 arrays of 5 int s. In particular, there is no single object in the memory, that is, the matrix ma - only elements are stored. Sizes 3 and 5 exist only in the compiler source.
(a main attention).
In other words, the notation [][]...[] is a compiler construct; syntactic sugar if you want.
For entertainment purposes, I wrote the following code:
#include<cstdlib> #include<iostream> #include<iterator> #include<algorithm> int main() { double ma[5][3]; double *beg = &ma[0][0]; // case 1 //double ma[3][5]; double *beg = &ma[0][0]; // case 2 //double ma[15]; double *beg = &ma[0]; // case 3 double *end = beg + 15; // fill array with random numbers std::generate(beg, end, std::rand); // display array contents std::copy(beg, end, std::ostream_iterator<double>(std::cout, " ")); std::cout<<std::endl; return 0; }
And compared the assembly generated for three cases using the compilation command (GCC 4.7.2):
g++ test.cpp -O3 -S -oc1.s
Cases are called c1.s , c2.s and c3.s The result of the shasum *.s command is:
5360e2438aebea682d88277da69c88a3f4af10f3 c1.s 5360e2438aebea682d88277da69c88a3f4af10f3 c2.s 5360e2438aebea682d88277da69c88a3f4af10f3 c3.s
Now I must mention that the most natural construction seems to be a one-dimensional declaration of ma , that is: double ma[N] , because then the starting position is just ma , and the ending position is just ma + N (this is in contrast to the address of the first element of the array).
I find that the algorithms provided by the <algorithm> C ++ Standard Library header are much more complex in this case.
Finally, I should recommend you use std::array or std::vector , if at all possible.
Greetings.