在 C++ 中处理巨大的多维数组

我正在用 C++ 设计一个类似于 Minecraft 的游戏,它在内存中保存了大量的地形数据.一般来说,我想将一个数组存储在 [5][4][5][50][50][50] 的内存中.这还不错,因为它相当于大约 100mb 的虚拟内存,因为我的结构只有大约 8 个字节.

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][50][50]. This isn't bad since it amounts to about 100mb of virtual memory since my structure will only be about 8 bytes.

但是,我无法找出处理此问题的最佳方法.我确实希望它在虚拟内存中,但显然不在堆栈中.而且我一直在犯一些如何在堆栈上创建这个数组并导致堆栈溢出的错误.我想做的是下面.这只是我拼凑的代码,为您提供我正在做的示例,我的机器上有语法正确的代码,我只是不想弄乱帖子.

However, I'm having trouble figuring out the best way to handle this. I do want this to be in virtual memory, but obviously not on the stack. And I keep making the mistake some how of creating this array on the stack an causing a stack overflow. What I would like to do is below. This is just code that I threw together to give you an example of what I'm doing, I have code with correct syntax on my machine, I just didn't want to clutter the post.

typedef struct modelBlock
{
    // Information about the blocks
} BLOCK;

typedef struct modelGrid
{
    bool empty;

    BLOCK blocksArray[50][50][50];

} GRID;


class Parent
{
     Child* child;
     Parent(void);
}

Parent::Parent()
{
    Child c;
    child = &c;
}

class Child
{
     GRID grids[5][4][5];
}

但是,每次我这样做时,都会导致堆栈溢出(适当的网站选择对吗?).我玩过使用基于指针的数组,但是数据在其范围之外丢失时遇到了很多麻烦.

However, every time I do this, I cause a stack overflow (appropriate web site choice right?). I played with using pointer based arrays, but I had a lot of trouble with data being lost outside of its scope.

如果有人能给我一些关于如何让我的数据存储在堆而不是堆栈上的见解,或者如果我应该使用其他方式来创建我的数组,我将非常感谢您的帮助.由于开销,我想避免使用向量,但我不确定它有多大.

If anyone could give me some insight on how to get my data to store on the heap instead of the stack, or if I should use some other way of creating my array, I'd really appreciate the help. I'd like to avoid using vectors because of overhead, though I'm not sure how substantial it is.

推荐答案

使用boost::multi_array

相关文章