11

OK, I know that in C++ a - let's say 2-dimensional - array can be initialized this way :

int theArray[5][3] = { 
    {1,2,3},
    {4,5,6},
    {7,8,9},
    {10,11,12},
    {13,14,15} 
};

Now, what if I want to use pre-existing arrays as theArray's elements?

E.g.

// A, B, C, D,... have already been declared as :
// `const U64 A[] = { 1,2,3,4 };` etc...

const U64 multiDimArray[12][64] = { 
     A, B, C, D, E, F,  
     G, H, I, J, K, L
};

This one, throws an error though :

cannot initialize an array element of type 'const U64' 
(aka 'const unsigned long long') with an lvalue of type 'const U64 [64]'

I see the point, but hopefully you can see mine.

Is there a workaround so that I can easily achieve the same thing? (Any suggestion - perhaps something using Boost? - is welcome)

0

3 Answers 3

6

If you use C++11, the initializer list for an array is flexible:

std::array<std::array<U64, 64>, 12> multiDimArray = {
     A, B, C, D, E, F,  
     G, H, I, J, K, L
};

will work fine, assuming A..L are std::array<64, U64>.

The array does have no overhead to the c-style array. Click here for official reference.

"The size and efficiency of array<T,N> for some number of elements is equivalent to size and efficiency of the corresponding C-style array T[N]." (From the reference)


I said "flexible", since you can use a mixed initializer-list like this:

std::array<int, 3> first_row = {1,2,3};
std::array<std::array<int, 3>, 2> a={
  first_row,
  {2, 2, 2}
};

You can use this as a fixed-size array, with the same operations:

a[1][2]=2; a[0][1]=1; a[0][2]=3;
Sign up to request clarification or add additional context in comments.

1 Comment

And you can use the new array class for fixed size array.
5

I can see why this is useful, however, in C, using just the variable name of an array will return the address of the array in memory. The compiler has no idea as to what is actually stored at A during compile time, so this wouldn't work.

As an alternative, what you could do is either use a memcpy and copy the elements into the array (but then it won't be const), or you could use a #define A { 1, 2, 3, 4 } and then maybe do something like:

#define A_val { 1, 2, 3, 4 }
#define B_val { 5, 6, 7, 8 }
const U64 multiDimArray[12][64] = {
    A_val,
    B_val,
    // and so on and so forth
}
const U64 A[4] = A_val; // if you need this
const U64 B[4] = B_val; // you can do it like this

1 Comment

This sounds like a really interesting idea. Let me see how I could fit this into my general code scheme. ;-)
2

You can use the fact that a two dimensional array is actually a one dimensional array of pointers to your advantage here. The follow initialization should work for you.

const U64 *multiDimArray[12] = { 
     A, B, C, D, E, F,  
     G, H, I, J, K, L
};

1 Comment

"actually a one dimensional array of pointers" They aren't. Yours is, but yours isn't a true 2D array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.