Some similar recent threads have reminded me that I've been wanting to ask this for a while. I would like to declare a 3-dimensional array that uses contiguous memory so that I can calculate memory offsets from the base array element ( x[0][0][0]
) and use pointer arithmetic to access any array element I want. This pointer arithmetic requires that the memory allocated for the array is guaranteed to be contiguous. This seems to be possible when the array dimensions are known at compile time, but I've been unsuccessful at doing so when those dimensions are known only at runtime. Here is my program. It dynamically allocates memory, then statically allocates it. The address and value of each element is displayed. The static array elements are contiguous. The dynamic array elements are not. Dynamic array allocation code is lines 17 through 26.
#include <iostream>
using namespace std;
int main ()
{
const int DIM1 = 2;
const int DIM2 = 3;
const int DIM3 = 4;
int dim1, dim2, dim3;
cout << "Enter dimension 1 of array: ";
cin >> dim1;
cout << "Enter dimension 2 of array: ";
cin >> dim2;
cout << "Enter dimension 3 of array: ";
cin >> dim3;
int*** x;
x = new int**[dim1];
for (int i = 0; i < dim1; i++)
{
x[i] = new int*[dim2];
for (int j = 0; j < dim2; j++)
{
x[i][j] = new int[dim3];
}
}
for (int i = 0; i < dim1; i++)
{
for (int j = 0; j < dim2; j++)
{
for (int k = 0; k < dim3; k++)
{
x[i][j][k] = 10000 * i + 100 * j + k;
}
}
}
cout << "\nDynamic array display\n";
cout << 'i' << '\t' << 'j' << '\t' << 'k' << '\t';
cout << "&x[i][j][k]" << '\t' << "x[i][j][k]\n\n";
for (int i = 0; i < dim1; i++)
{
for (int j = 0; j < dim2; j++)
{
for (int k = 0; k < dim3; k++)
{
cout << i << '\t' << j << '\t' << k << '\t';
cout << &x[i][j][k] << '\t' << x[i][j][k] << endl;
}
}
}
int y[DIM1][DIM2][DIM3];
for (int i = 0; i < DIM1; i++)
{
for (int j = 0; j < DIM2; j++)
{
for (int k = 0; k < DIM3; k++)
{
y[i][j][k] = 10000 * i + 100 * j + k;
}
}
}
cout << "\nStatic array display\n";
cout << 'i' << '\t' << 'j' << '\t' << 'k' << '\t';
cout << "&y[i][j][k]" << '\t' << "y[i][j][k]\n\n";
for (int i = 0; i < DIM1; i++)
{
for (int j = 0; j < DIM2; j++)
{
for (int k = 0; k < DIM3; k++)
{
cout << i << '\t' << j << '\t' << k << '\t';
cout << &y[i][j][k] << '\t' << y[i][j][k] << endl;
}
}
}
return 0;
}
I think I know what the problem is, but I don't know the solution. Here is the output when I enter 2, 3, and 4 respectively for the dimensions. Note the red lines below. Those are examples of array elements which are not contiguous in memory. Is there a way to declare this array so that this does not happen?
Enter dimension 1 of array: 2
Enter dimension 2 of array: 3
Enter dimension 3 of array: 4
Dynamic array display
i j k &x[i][j][k] x[i][j][k]
0 0 0 0x3e24c8 0
0 0 1 0x3e24cc 1
0 0 2 0x3e24d0 2
0 0 3 0x3e24d4 3
0 1 0 0x3e24e0 100
0 1 1 0x3e24e4 101
0 1 2 0x3e24e8 102
0 1 3 0x3e24ec 103
0 2 0 0x3e24f8 200
0 2 1 0x3e24fc 201
0 2 2 0x3e2500 202
0 2 3 0x3e2504 203
1 0 0 0x3e2528 10000
1 0 1 0x3e252c 10001
1 0 2 0x3e2530 10002
1 0 3 0x3e2534 10003
1 1 0 0x3e2540 10100
1 1 1 0x3e2544 10101
1 1 2 0x3e2548 10102
1 1 3 0x3e254c 10103
1 2 0 0x3e2558 10200
1 2 1 0x3e255c 10201
1 2 2 0x3e2560 10202
1 2 3 0x3e2564 10203
Static array display
i j k &y[i][j][k] y[i][j][k]
0 0 0 0x22fee0 0
0 0 1 0x22fee4 1
0 0 2 0x22fee8 2
0 0 3 0x22feec 3
0 1 0 0x22fef0 100
0 1 1 0x22fef4 101
0 1 2 0x22fef8 102
0 1 3 0x22fefc 103
0 2 0 0x22ff00 200
0 2 1 0x22ff04 201
0 2 2 0x22ff08 202
0 2 3 0x22ff0c 203
1 0 0 0x22ff10 10000
1 0 1 0x22ff14 10001
1 0 2 0x22ff18 10002
1 0 3 0x22ff1c 10003
1 1 0 0x22ff20 10100
1 1 1 0x22ff24 10101
1 1 2 0x22ff28 10102
1 1 3 0x22ff2c 10103
1 2 0 0x22ff30 10200
1 2 1 0x22ff34 10201
1 2 2 0x22ff38 10202
1 2 3 0x22ff3c 10203