What do "new int[2][3]" ?

What do and what data type return this expresion ?

 
new int[2][3]


I have tried this:

 
int* p = new int[2][3];


But it causes a compile time error: It can't be done the conversion from 'int (*)[3]' to 'int *'.

If I use reinterpret_cast it compiles:

 
int* p = reinterpret_cast<int*>(new int[2][3]);


But, what is pointed by p?

Thanks.
p points to an array of length 6. new int[2][3] is a "2D" array of int; with 2 rows and 3 columns. I.e. the array contains 2 elements and each element of the array is an array of 3 ints.

To store this in a variable, you need a suitable pointer. So int **p = new int[2][3]. The reason why your code worked is because multi dimensional arrays are represented as 1D arrays, but when accessed with the [][] the compiler does a simple arithmetic to determine which element to return.
I don't think it's that simple, Smac89.
> What do and what data type return this expresion ?
> new int[2][3]

Write it this way, and it's easy.

1
2
3
4
typedef int array_type[3] ; 
// or: using array_type = int[3] ;

array_type* pointer_to_array = new array_type[2] ; // new int[2][3] 


@L B

¿What makes it not simple? The machine instructions?
This sentence:

 
int **p = new int[2][3];


Produces a compile time error: It can't be done the conversion from 'int (*)[3]' to 'int **'.

If it is used the operator reinterpret_cast:

 
int **p = reinterpret_cast<int**>(new int[2][3]);


Compiles without errors.

This other sentence:

 
int (*p)[3] = new int[2][3];


Compiles without errors too and it is not necesary to use the operator reinterpret_cast.
reinterpret_cast only silences the error, it does not fix the undefined behavior.
Here is the solution:

1
2
3
4
5
6
7
8
9
10
11
// m is a matrix of 2 dimensions whose dimensions are {2, 3}.
// Internally, is a matrix of 1 dimension whose elements are stored by rows (first row 0,
// second row 1)
int m [2][3] = {{1, 2, 3},
                {4, 5, 6}};

for (int i = 0; i < 2; i++){
	for (int j = 0; j < 3; j++){
		cout << "m[" << i << "][" << j << "]: " << m[i][j] << endl;
	}
}


Output:


m[0][0]: 1
m[0][1]: 2
m[0][2]: 3
m[1][0]: 4
m[1][1]: 5
m[1][2]: 6


1
2
3
4
5
6
7
8
// pm is a pointer to a matrix of 1 dimension whose dimension is {1}
int (*pm)[3] = m;

for (int i = 0; i < 2; i++){
	for (int j = 0; j < 3; j++){
		cout << "pm[" << i << "][" << j << "]: " << pm[i][j] << endl;
	}
}


Output:


pm[0][0]: 1
pm[0][1]: 2
pm[0][2]: 3
pm[1][0]: 4
pm[1][1]: 5
pm[1][2]: 6


1
2
3
4
5
6
7
// pm2 is a pointer to an int that pointer to the first element of the matrix m, this is, to the
// element m[0][0]
int* pm2 = reinterpret_cast<int*>(m);

for (int i = 0; i < 6; i++){
	cout << "pm2[" << i << "]: " << pm2[i] << endl;
}


Output:


pm2[0]: 1
pm2[1]: 2
pm2[2]: 3
pm2[3]: 4
pm2[4]: 5
pm2[5]: 6


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// new int[2][3] reserve memory dinamycally for one matrix of 2 dimensions whose
// dimensions are {2, 3}.
// Internally, is a matrix of 1 dimension whose elements are stored by rows (first row 0,
// second row 1)
pm = new int[2][3];

int contador = 1;
for (int i = 0; i < 2; i++){
	for (int j = 0; j < 3; j++){
		pm[i][j] = contador;
		contador++;
	}
}
for (int i = 0; i < 2; i++){
	for (int j = 0; j < 3; j++){
		cout << "pm[" << i << "][" << j << "]: " << pm[i][j] << endl;
	}
}

delete [] pm;


Output:


pm[0][0]: 1
pm[0][1]: 2
pm[0][2]: 3
pm[1][0]: 4
pm[1][1]: 5
pm[1][2]: 6


1
2
3
4
5
6
7
8
9
10
11
12
pm2 = reinterpret_cast<int*>(new int[2][3]);

contador = 1;
for (int i = 0; i < 6; i++){
	pm2[i] = contador;
	contador++;
}
for (int i = 0; i < 6; i++){
	cout << "pm2[" << i << "]: " << pm2[i] << endl;
}

delete [] pm2;


Output:


pm2[0]: 1
pm2[1]: 2
pm2[2]: 3
pm2[3]: 4
pm2[4]: 5
pm2[5]: 6

Last edited on
Topic archived. No new replies allowed.