Traversing a 2D array

W

wallacej

Hi

I have a 2D array : imageTileArray and I would like to traverse it i.e.
visit every entry in the array sequentially. I've had a go at doing it
myself as follows but i get address violations:


for(i=0; i < sizeof(imageTileArray); i++) {
for(j=0; j < sizeof(imageTileArray[j]); j++) {
//do something
}
}

Is this the correct way to do so or is there better/correct way.

thank You

wallace
 
K

Kai-Uwe Bux

wallacej said:
Hi

I have a 2D array : imageTileArray and I would like to traverse it i.e.
visit every entry in the array sequentially. I've had a go at doing it
myself as follows but i get address violations:


for(i=0; i < sizeof(imageTileArray); i++) {
for(j=0; j < sizeof(imageTileArray[j]); j++) {
//do something
}
}

Is this the correct way to do so or is there better/correct way.


No, it is not correct.

First, from your post, it is not clear whether the arrays are statically or
dynamically allocated. The biggest problem with the latter is that they
simply do not keep track of their length. The former do, but to get at that
information in a generic fashion one needs templates.

As a solution for your problem, I would suggest to use std::vector instead.

typedef std::vector< imageTile > imageTileRow;
typedef std::vector< imageTyleRow > imageTileMatrix;

for ( imageTyleMatrix::const_iterator row_iter = matrix.begin();
row_iter != matrix.end(); ++row_iter ) {
for ( imageTypeRow::const_iterator col_iter = row_iter->begin();
col_iter != row_iter->end(); ++ col_iter ) {
// do something to *col_iter
}
}

If you use static allocation, you may want to replace std::vector by
boost::array.


Best

Kai-Uwe Bux
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

wallacej said:
Hi

I have a 2D array : imageTileArray and I would like to traverse it i.e.
visit every entry in the array sequentially. I've had a go at doing it
myself as follows but i get address violations:


for(i=0; i < sizeof(imageTileArray); i++) {
for(j=0; j < sizeof(imageTileArray[j]); j++) {
//do something
}
}

Is this the correct way to do so or is there better/correct way.

thank You

wallace


Add the following lines to your code snipet and make it compile and
run, you'll learn a lot:

int a[5][7];
std::cout << sizeof(int) << "\n"; // 4
std::cout << sizeof(a[0]) << "\n"; // you'd like 5, I got 28!
std::cout << sizeof(a[0][0]) << "\n"; // you'd like 7, I got 4!

Regards, Stephan
 
G

Gernot Frisch

I have a 2D array : imageTileArray and I would like to traverse it
i.e.
visit every entry in the array sequentially. I've had a go at doing
it
myself as follows but i get address violations:

if it's an array on the stack:
for(i=0; i < sizeof(imageTileArray)/sizeof(imageTileArray[0]);
i++) {
for(j=0; j <
sizeof(imageTileArray[j])/sizeof(imageTileArray[0][0]); j++) {
//do something
}
}


if it's a heap array (using new/malloc) - uh-oh!

This is C++, so _do_ use std::vector as told by Kai-Uwe.
 
W

wallacej

thanks for all the help guys, the arrays are dynamic so i'll give
vectors a try.

wallace
 
R

roberts.noah

wallacej said:
thanks for all the help guys, the arrays are dynamic so i'll give
vectors a try.

wallace

One disadvantage of using vector<vector<>> is that your items won't be
contiguous. Your rows will be but not the rest. You can't do
something like this:

for (int i = 0; i < ROWSIZE * COLSIZE; ++i)
visit(array + i);

This can, depending on your needs, better fit the algorithms you want
to perform.

You can still access it like a two dim array of sorts:

visit(array + a * ROWSIZE + b);

You can of course hide this type of access in an operator, if you
encapsulate your storage in a class, like so:

....
T & operator()(int a, int b) { return array + a * ROWSIZE + b; }

visit(darray(a,b)); // note that this visit accepts a different type
than the other two.

or

T & elementAt(int a, int b)....

You can do operator[] to return a pointer to your row but that is a
hack best avoided if not absolutely necessary. Even if such syntax is
necessary returning some sort of adapter and/or proxy instead of a
pointer would be much better but more complex to develop.
 
H

Howard

wallacej said:
thanks for all the help guys, the arrays are dynamic so i'll give
vectors a try.

wallace

If the arrays are allocated dynamically, then you should already know the
number of elements (i.e., length and width). You could just keep those two
values around and use them. (Or, if each sub-array is a different size, you
can keep their individual lengths in a separate array.)

-Howard
 
W

wallacej

thanks for the help there guys

i took howards advice and it has worked. as usual a simple solution
was staring me in the face and i only had to change a minor part of my
code.

Cheers
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top