I'll show you how to do it using an array of pointers. Each element of the array is a pointer to another array. This allows you to do the [] indexing for all the elements. Since there are multiple new statements, you will probally want a seperate function to delete the memory. This will need to know the values for m and n so it knows what to free
Doing so much memory maintance can be annoying and error prone, so it may be a good idea to use STL to handle as much as possible in a memory safe way
Another idea is to use a class to handle everything for you. This class can remember to dimensions so that it can be deleted correctly
Also, of note, the following method has a memory overhead of (m * n + m + 1) * sizeof(void*). An alternative is to create a 1D array with m * n * l elements and index it as i * n * l + j * l + k. Then, you can delete it with a simple call to delete []
int** create2Darray(int n, int l)
int** array
int j
array = new int**[n]
for (j = 0; j < n; j++)
array[j] = new int[l]
return array
int*** create3Darray(int m, int n, int l)
int*** array
int i
array = new int**[m]
for (i = 0; i < m; i++)
array = create2Darray(n, l)
return array
void delete3Darray(int*** array, int m, int n)
int i, j
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
delete [] array[j]
delete [] array
delete [] array
void use3Darray()
int *** array
int m = 10
int n = 20
int l = 5
array = create3Darray(m, n, l)
array[2][3][4] = 5
array[1][3][5] = 7
array[0][0][0] = array[2][3][4] + array[1][3][5]; /* array[0][0][0] = 12 *
delete3Darray(m, n)
}