1

I have several large arrays of 64-bit integers whose values are expensive to calculate, so I presently have generator codes in my build system that calculate the constants and generate the .cpp files to initialize the arrays. Some of them are 1d, but many of them are 2d. Previously, my generator scripts have declared them thus (the values are calculated by my generator code)

   namespace common{ namespace bitboards{

   /////    Additional stuff

   const bitboard ray_thr_sqs[64][64] = {
                     {0x81412111090503fe, 0xfc, 0xfa, 0xf6, 0xee,......

   };};

but I want to use to do it in a more C++ without C like arrays. (A few of them are bigger than this with size[4096][64]). For the 1d arrays, I can just use std::array. For the 2d ones, I don't want to use nested std::arrays for performance reasons, but would rather use boost multi_array.

How can I define a const boost multi_array? I have tried this:

    //file1.cpp
    namespace common{ namespace bitboards{

    ///////additional stuff

    const boost::multi_array<bitboard, 2> ray_thr_sqs = misc::boost_help::makeMultiArray<bitboard, 64, 64>({0x81412111090503fe, 0xfc, 0xfa, 0xf6, 0xee,......

    };};

where makeMultiArray is a helper function to generate the arrays and bitboard is a typedef for uint64_t. (This helper function takes a flat initializer list) This compiles, but when I come to run it, the array is empty. I know why it is empty, and it's pretty obvious after the fact: makeMultiArray is not being run, but I can't think of another way to do what I want without staying with C like arrays or nested std::arrays.

Is there a way to do this with constexpr or something, or a better way of doing it entirely without using my generator code.

edit: These are declared at namespace scope in a .cpp file.

edit 2 A commenter below now suspects a linking problem, rather than what I initially suspected, and I now agree. I have tried a minimal working example with the makeMultiArray function above, and it does work, so the problem is elsewhere. I have added the names of the namespaces in the above. Also, the declaration in the header file is thus:

    //header.hpp
    namespace common{ namespace bitboards{ 

    //This const is definied in generated file1.cpp above which I generate in my build scripts because the are large arrays and expensive to calculate
    extern const boost::multi_array<bitboard,2> ray_thr_sqs;


    //This is another constant array that is much smaller and I can write by hand in a second cpp file, file2.cpp
    extern const std::array<bitboard, 8> ranks;
    };};

Here is file2.cpp

    namespace common{ namespace bitboards{

    //////Additional stuff
     const std::array<bitboard, 8> ranks = {0xff, 0xff00, 0xff0000, 0xff000000, 
     0xff00000000, 0xff0000000000, 0xff000000000000, 0xff00000000000000};

     };};

Could the definition being in two separate cpp files be causing my issue?

4
  • Are you declaring this in a variable at namespace scope in a cpp file? Commented Nov 12, 2019 at 5:03
  • Yes, neglected to mention that. Edited Commented Nov 12, 2019 at 7:25
  • Can you post the exact declaration in the corresponding header file? I suspect the problem has to do with linkage, rather than a boost usage problem. stackoverflow.com/a/11478458/1718575 Commented Nov 12, 2019 at 18:13
  • I concur. I have done a minimum working example with my makeMultiArray function, and it does work in my example, so I have added additional detail above. In particular, there are several of these constant arrays. There are the large ones that are expensive to calculate that I generate in my build script in one cpp file, and some that are much smaller that I define by hand in a separate cpp file. Could the definition in two separate files be causing my issue? Commented Nov 12, 2019 at 19:50

1 Answer 1

1

It turns out that it was a bug elsewhere. At one point where I use the array, there was an error in my calculation of the indices that was leading it to be out of bounds. The verbose error I was getting from boost::multi_array was what led me to suspect the array was completely uninitialized, when actually it was an out of bounds problem. Thanks for your help.

Sign up to request clarification or add additional context in comments.

Comments

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.