2 dimensional arrays

F

Francis Glassborow

Ivan Korotkov said:
Or better this:

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

The former method loses in performance (because each array access needs
two derefernces). The latter one doesn't, it's just the same as { int
pi[2][3]; }

I'd prefer to see '3' replaced. Either by:

int const row_length(3);

or:

typedef int row[3];
row * array(new row[2]);

Or even better, do both:

int const row_length(3);
typedef int row[row_length];
int const rows(2);
row * array(new row[rows]);


typedefs are a good way to handle 'magic' types.
 
K

kanze

Ivan Korotkov said:
Try this:
int** arr2 = new int*[2];
for(int i=0; i<2;i++)
arr2 = new int[3];

Or better this:
int (*pi)[3] = new int[2][3];
The former method loses in performance (because each array access
needs two derefernces). The latter one doesn't, it's just the same as
{ int pi[2][3]; }

The performance depends on the machine. I've worked on machines where
the multiplication was significantly slower than a memory access, and
where the first solution would have been significantly faster.

Personally, if I needed such a two dimensional array, I'd start with:

std::vector< std::vector< int > > arr( 2, std::vector< int >( 3 ) ) ;

Only if the performance wasn't adequate, and the profiler showed that
std::vector was my bottleneck, would I try anything else.
 
Joined
Nov 11, 2007
Messages
1
Reaction score
0
Here is a solution using pointers.
int rows=2, cols=3, cntr=0, *array1D, **array2D;
array1D = new int[rows*cols];
array2D = new int*[rows];
for (int k=0; k<rows; k++) array2D[k] = array1D+k*cols;

for (int i=0; i<rows; i++) for (int j=0; j<cols; j++) array2D[j] = cntr++;
cout << "\n1-by-" << rows*cols << " 1D array :\n[";
for (int k=0; k<(rows*cols); k++) cout << array1D[k] << " ";cout << "\n";
cout << "\n" << rows << "-by-" << cols << " 2D array: \n[";
for (int i=0; i<rows; i++) {for (int j=0; j<cols; j++) cout << array2D[j] << " ";cout << "]\n ";}

delete[] array2D;
delete[] array1D;

If you want to try with 3D array then it would be as follows.

int bands=2, rows=2, cols=3, cntr=0, *array1D, **array2D, ***array3D;
array1D = new int[bands*rows*cols];
array2D = new int*[bands*rows];
array3D = new int**[bands];
for (int k=0; k<bands; k++) for (int i=0; i<rows; i++) array2D[k*rows+i] = array1D+(k*rows+i)*cols;
for (int k=0; k<bands; k++) array3D[k] = array2D+k*rows;

for (int k=0; k<(bands*rows*cols); k++) array1D[k]=cntr++;

cout << "\n1-by-" << rows*cols*bands << " 1D array :\n[";
for (int k=0; k<(rows*cols*bands); k++) cout << array1D[k] << " ";cout << "\n";
cout << "\n" << bands << "-by-" << rows << "-by-" << cols << " 3D array: \n";
for (int k=0; k<bands; k++) {
cout << "\nBand[" << k << "]\n[";
for (int i=0; i<rows; i++) {for (int j=0; j<cols; j++) cout << array3D[k][j] << "\t";cout << "]\n ";}
}

delete[] array3D;
delete[] array2D;
delete[] array1D;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,038
Members
47,633
Latest member
BriannaLyk

Latest Threads

Top