Best way to declare (C++) array ?

Started by
18 comments, last by darren_mfuk 17 years, 1 month ago
What is the better way to declare an array ??? myArray[100][100] or myArray[100 * 100] ????? If it matters, it's to declare a tiles grid (cols by rows) and to populate an array with the data before rendering. Cheers
----------------------------------------Now just hit that link that says 'Rate This User' [wink]
Advertisement
Quote:Original post by darren_mfuk
What is the better way to declare an array ???

myArray[100][100]

or

myArray[100 * 100]

?????

If it matters, it's to declare a tiles grid (cols by rows)
and to populate an array with the data before rendering.

Cheers


If you use myArray[100][100] I believe c++ will internally use myArray[100*100] and will do the correct index math.
The two declare different things. Ultimately, you must decide what object you need, and then use the corresponding declaration.

So, do you need a one-dimensional array, or a two-dimensional array? Judging by your mention of a grid, you seem to want the two-dimensional array. The only way of declaring a two-dimensional array is through:

myArray[100][100]

I'd probably do the first one, it's largely a matter of taste.

Whichever you do, wrap up the array into an object so that you can guarantee there's no accidental reading/writing past the bounds. Ensuring safety is always A Good Thing.
The second method is what I prefer to use, though I'm not exactly sure if there is any major performance benefit to it.

In actuality, I would skip using the pointer method altogether and use a vector container.

ie:

#include <iostream>using std::cout;#include <vector>using std::vector;void func(int *x){  cout << *x;}int main(void){ size_t x_size = 100; size_t y_size = 100; vector<int> vint; vint.reserve(x_size*y_size); // must reserve the memory first vint.resize(x_size*y_size); // now resize // indexing example  for(size_t i = 0; i < x_size; i++)  for(size_t j = 0; j < y_size; j++)   vint[j*y_size + i] = 3; // at and array notation vint.at(0) = 1; // using a method which guards against out of bounds vint[1] = 2; // does not guard against out of bounds, so be careful // using address-of operator to pass in address of element as pointer func(&vint[0]); func(&vint[1]); func(&vint[2]); // vector is deallocated for you automatically when it goes out of scope // (no delete[] / free() required like with a pointer) return 0;}
Quote:The second method is what I prefer to use, though I'm not exactly sure if there is any major performance benefit to it.

Of course there isn't any performance benefit at all. If it would give better performance then the compiler would transform it itself. Of course the reverse may be true, that if it knows it's dealing with a multidimensional array then it may utilize optimizations specific to those. I don't really know of any however, so I doubt it will be noticeable at all.

Ultimately, what it comes down to is what message you want to convey to both other programmers and the compiler. If I saw an uncommented declaration of an array of 10000 elements I would assume it was one-dimensional, not a hack for a two-dimensional array and therefore it would hurt my understanding of your code.

Of course the one-dimensional alternative have some benefits (they aren't performance though), for instance how would you pass a multidimensional-array to a standard algorithm?

If you want the best of both alternatives (and some extra features in addition), I suggest you check out Boost.MultiArray. Better than any of the original alternatives.

[Edited by - CTar on March 1, 2007 10:09:36 AM]
First of all to taby:
You don't have to use reserve before resize. Look it up in Stroustrup's book.

For the original question, any decent compiler will make as efficient code for one as for the other. It's not unlikely that it will even generate identical code.

Just use the one you are more comfortable with. If you want to sweep through the whole array many times probably type array[100*100] is better, but if you wish to make a lot of random access to you array the other one is more convenient.

shinjin
ok,

well 2 dimensional seems the obvious choice here for a grid.

So lets widen the topic a little, although it is an openGL problem

I am creating a (hexagonal) grid using

for 0 to cols
for 0 to rows
drawHexTile();
}
}

where we would call drawHexTile() 100 times, for a 10x10 grid ok ?

drawHexTile() actually draws a TRIANGLE_FAN to form a hexagon.
of cause this is doing it all EVERY single frame.

So it was suggested that I populate an array, just once with all the hexagon co-ords, before render.

Heres the thread:
Original Post
----------------------------------------Now just hit that link that says 'Rate This User' [wink]
Quote:Original post by crshinjin
If you want to sweep through the whole array many times probably type array[100*100] is better, but if you wish to make a lot of random access to you array the other one is more convenient.
shinjin


How do you figure?
OK guys,

Since there seems to be great dispute and opinion here.

Please check out the Original thread, for a complete history of the task at hand

Here's the thread:
Original Post

There has to be a more structured and faster solution for this problem

Thanks ;-)

----------------------------------------Now just hit that link that says 'Rate This User' [wink]

This topic is closed to new replies.

Advertisement