Compiler hang when initializing large std::arrays

前端 未结 3 1918
清歌不尽
清歌不尽 2021-01-12 16:21

I need to initialize a very large multidimensional std::array of data:

class Thing;

class World
{
public:
    World() : space{nullptr} {};
             


        
相关标签:
3条回答
  • 2021-01-12 16:38

    Ok, i can't explain the nuance of why g++ is puking on this, but until you work that out, consider this for your member declaration:

    std::vector<std::array<std::array<std::unique_ptr<Thing>,size>,size>> space;
    

    and in the constructor initializer list:

    World() : space{size}
    

    That should at least get you compiling and move all this to the heap. Note: this better be a 64bit process. I'm going to have to hunt to find out why g++ is doing what I suspect it is doing.

    0 讨论(0)
  • 2021-01-12 16:44

    As you are using unique_ptr it looks like you are looking for a sparse 3d-matrix like type. For implementing a sparse matrix you could have a look at What is the best way to create a sparse array in C++? and as an implementation detail you could use Boost Multi-index for implementing fast access to all dimensions.

    0 讨论(0)
  • 2021-01-12 16:48
    vector<vector<vector<unique_ptr<Thing>>>> space;
    

    and when initializing it:

    for (int i = 0; i < 1000; i++)
    {
       vector<vector<unique_ptr<Thing>>> sp1;
       for (int j = 0; j < 1000; j++)
       {
          vector<unique_ptr<Thing>> sp2;
          for (int k = 0; k < 1000; k++)
             sp2.push_back(make_unique<Thing>("params", "to", "construct", "thing"));
    
           sp1.push_back(move(sp2));
       }
    
       space.push_back(move(sp1));
    }
    

    It is similar to your approach, but it constructs the vectors in the heap instead of stack.

    0 讨论(0)
提交回复
热议问题