Initializing a multidimensional array

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
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().
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
They are equivalent regardless of size, as long as the 2d array is not dynamically allocated.
Topic archived. No new replies allowed.