cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
Initializing a multidimensional array
Initializing a multidimensional array
Jun 22, 2010 at 11:24pm UTC
tfcoder
(18)
What is the recommended way to initialize a multidimensional array?
Someone commented that the following is "not optimal".
1
2
3
for
(
int
i = 0; i < rows; i++)
for
(
int
j = 0; j < cols; j++) a[i][j] = 0;
If relevant, please compare memset() and ZeroMemory() in this context.
Thank you for your help in advance.
Last edited on
Jun 22, 2010 at 11:28pm UTC
Jun 23, 2010 at 3:10am UTC
PanGalactic
(1658)
As this is C++, the recommended way is to use a multi-dimensional container type and have the container worry about initialization.
http://www.boost.org/doc/libs/1_43_0/libs/multi_array/doc/reference.html
If you are writing a multi-dimensional container, then the answer is "it depends on what types are stored in the container." One can specialize the initialization of POD types with memset(). But non-POD types cannot be initialized with memset().
Jun 23, 2010 at 9:15pm UTC
tfcoder
(18)
Thank you. I must admit that I don't entirely understand what you said, I will have to do some reading I guess.
Is there any significant performance difference between a [row][col] addressing system vs [row*RowSize + col]?
For now my arrays are small (about 200 integers). Would the answer be different if I had about 5000 floats?
Last edited on
Jun 23, 2010 at 9:16pm UTC
Jun 24, 2010 at 1:02am UTC
jsmith
(5804)
They are equivalent regardless of size, as long as the 2d array is not dynamically allocated.
Topic archived. No new replies allowed.