Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ with big std::array freezes [duplicate]

Tags:

c++

arrays

c++11

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

class Thing;

class World
{
public:
    World() : space{nullptr} {};
    ~World() = default;
private:
    static unsigned int const size = 1000;
    std::array<std::array<std::array<std::unique_ptr<Thing>, size>, size>, size> space;
};

If you try to instantiate this, G++ 4.8.2 chokes: it consumes all the available memory and will not return. That is, the compiler hangs and I never get an executable. Why is this? Note that clang++ has no trouble.

Note: I fully realize that putting this much data on the stack can overflow it. What is the best way to initialize it on the heap? I think making space a reference (to allocated memory) would be the best way, but I can't figure the syntax out.

like image 440
George Hilliard Avatar asked Mar 25 '14 03:03

George Hilliard


2 Answers

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.

like image 82
WhozCraig Avatar answered Nov 16 '22 18:11

WhozCraig


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.

like image 23
Jan Herrmann Avatar answered Nov 16 '22 17:11

Jan Herrmann