在C ++中处理巨大的多维数组 [英] Handling Huge Multidimensional Arrays in C++

查看:88
本文介绍了在C ++中处理巨大的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个类似于Minecraft的C ++游戏,该游戏在内存中保存了大量的地形数据.通常,我想在内存中存储一​​个数组,该数组为[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

这篇关于在C ++中处理巨大的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆