Handling Huge Multidimensional Arrays in C++

前端 未结 8 2003
别那么骄傲
别那么骄傲 2021-01-24 19:15

I\'m designing a game in C++ similar to Minecraft that holds an enormous amount of terrain data in memory. In general, I want to store an array in memory that is [5][4][5][50][

8条回答
  •  逝去的感伤
    2021-01-24 19:52

    If you want to allocate something on the heap, use new.

    #include 
    
    class Parent
    {
        std::auto_ptr child; // use auto_ptr for dynamically-allocated members
        Parent(const Parent&); // You probably don't want to copy this giant thing
    public:
        Parent();
    };
    
    Parent::Parent()
      : child(new Child) // initialize members with an initializer list
    {
    }
    

    Also, avoid mixing C and C++ styles. There's no reason to do

    typedef struct blah{ ... } BLAH;
    

    in C++. A struct is just a class with all of the members public by default; just like a class, you can refer to the struct type's name without using the struct tag. There's also no need to specify void for a function that takes no parameters.

    boost::multi_array (linked in PigBen's answer) is a good choice over raw arrays.

提交回复
热议问题