简体   繁体   English

用数组减少c ++内存

[英]c++ Memory reduction with arrays

I'm creating a block based game and I would like to improve on its memory usage. 我正在创建一个基于块的游戏,我想提高其内存使用率。 I'm currently creating blocks with a sizeof() 8. And I can't reduce its size. 我当前正在使用sizeof() 8创建块。并且我无法减小其大小。

Block: 块:

...
bool front, back, left, right, top, bottom;//If the face is active, true

BYTE blockType;
BYTE data;

I came up with a solution but I have no idea how to properly implement it because I'm quite new to c++. 我想出了一个解决方案,但是我不知道如何正确实现它,因为我对C ++还是很陌生。 The solution: All air blocks are exactly equal but each take up 8 bytes of memory. 解决方案:所有空气块完全相等,但每个空气块占用8个字节的内存。 If I could set all air blocks, pointing to the same piece of memory, this should (I guess) be using less memory. 如果我可以将所有空气块都设置为指向同一块内存,那么(我想)应该使用更少的内存。 (unless the pointer address takes up 8 bytes ?) (除非指针地址占用8个字节?)

Currently my array looks like this: 目前,我的数组如下所示:

Chunk: 块:

Block*** m_pBlocks;

Chunk::Chunk()
{
    m_pBlocks = new Block**[CHUNK_SIZE];
    for(int x = 0; x < CHUNK_SIZE; x++){
        m_pBlocks[x] = new Block*[CHUNK_HEIGHT];
        for(int y = 0; y < CHUNK_HEIGHT; y++){
            m_pBlocks[x][y] = new Block[CHUNK_SIZE];
        }
    }
}

I know you can't make these point to null or point to something else so how should I do this? 我知道您不能使这些指向null或其他内容,所以我应该怎么做?

if you don't need to modify those blocks, you could create a lookup map. 如果您不需要修改这些块,则可以创建一个查找映射。 please, do not use new, and avoid pointers as much as you can. 请不要使用new,并尽可能避免使用指针。

bool lookup[CHUNK_SIZE][CHUNK_HEIGHT];

Chunk::Chunk()
{
    for(int x = 0; x < CHUNK_SIZE; x++)
      for(int y = 0; y < CHUNK_HEIGHT; y++)
        lookup[x][y] = true;
}

now you can just query the lookup table to see if you have that particular block set. 现在,您只需查询查找表即可查看是否具有该特定的块集。 Moreover you now have all those values close together, which is beneficial for performance. 此外,您现在将所有这些值放在一起,这对性能很有帮助。

Using bitfields to reduce the size of Block. 使用位域来减小块的大小。

class Block {
  // bit fields, reduce 6 bytes to 1
  unsigned char front:1, back:1, left:1, right:1, top:1, bottom:1;//If the face is active, true

  BYTE blockType;
  BYTE data;
  // optional alignment to size 4.
  // BYTE pad;
};

Block m_pBlocks[32][64][32]; // 32*64*32=64K * sizeof(Block)=256K that is a lot.

And yes, using a pointer that is 8 bytes is not really a save. 是的,使用8字节的指针并不是真正的节省。

But there are several methods that helps save more, if you have a hightmap everything above the hight map is air! 但是有几种方法可以帮助您节省更多,如果您有一个hightmap,hight地图上方的所有内容都是空中的! so you only need a 2d array to check that, where the elements are the hight. 因此,您只需要一个2d数组来检查元素在哪里最高。 All air voxels below the hightmap must be defined along with the other none-air elements. 必须在hightmap下方定义所有空中体素以及其他非空中元素。

Other data structures are often more space effective like Octree or Sparse voxel octree . 其他数据结构通常更节省空间,例如Octree稀疏体素八叉树

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM