As far as I know, dynamically allocated 2d arrays should be allocated like this:
int **rows = new int*[7];
//for loop assigning all seven rows[i] a "new int[4]"
However, the following also works for me:
int (*ptr)[4] = new int[7][4]; //create dynamic 2d array
for(int i = 0; i < 7; ++i) //initialize and test
{
for(int j = 0; j < 4; ++j)
{
ptr[i][j] = i + j;
cout << ptr[i][j] << ' ';
}
cout << endl;
}
delete[] ptr;
/*
output:
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
5 6 7 8
6 7 8 9
*/
Why should I use the more complicated method I mentioned first, when I can create a dynamical 2d array with this one line:
int (*ptr)[4] = new int[7][4];
I appreciate any insights!
(I did not find a memory leak using valgrind.)