c++ 12bit 变量,我该怎么做? [英] c++ 12bit variable, how can I do this?
问题描述
我正在构建一个体素引擎,所以我担心内存使用情况,使用 12 位而不是 16 位块 ID 可以节省大量内存.我有一个 3D 块 id 数组,每个 id 都有一个静态配置.
I'm building a voxel engine so I have concerns about memory usage, using 12bit instead of 16bit block ids could save a lot of memory. I have a 3D array of block ids, each id has a static configuration.
我不确定实现这一目标的好方法是什么.有没有办法只获取一块原始内存并自己管理它,这是一个好主意吗?我可能会遇到什么问题吗?有没有更好的方法来做到这一点?
I'm not sure what a good way to achieve this would be. Is there a way to just get a block of raw memory and manage it myself and would this be a good idea; are there any problems I might run into? Is there a better way to do this?
我对 c++ 有一些经验,但我大部分时间都花在了 Java 和 c# 上.
I have some experience with c++, but most of my time was spent with Java and c#.
感谢阅读!
推荐答案
这是可能的,例如:-
- 保留一个 8 字节的变量(存储 5 个变量,每个 12 位),或
- 使用
char[k]
保留k
~ceiling{(12*n)/8.0f}
,然后使用union等....
- reserve a 8-byte variable (store 5 variables, each 12 bits), or
- reserve using
char[k]
thatk
~ceiling{(12*n)/8.0f}
, then use union, etc....
这是我学习在块中分配内存的链接(我是提问者).
如何使用 void* 作为单个变量持有人?(例如 void* raw=SomeClass() )
Here is a link (I am the one who ask) that I learn about allocating memory in a chunk.
How to use void* as a single variable holder? (Ex. void* raw=SomeClass() )
不过,我有点不同意你想要做什么.
However, I moderately disagree of what you are trying to do.
据我所知,你的方法的缺点是:-
As far I know, the disadvantage of your approach are :-
- 一些错误:更难实现.
- 对齐问题:性能可能会更差.
看到这个:内存对齐:如何使用alignof/alignas?
传统推荐:在(过早地)优化之前对其进行分析.
Traditional recommendation : profile it before (prematurely) optimize it.
这篇关于c++ 12bit 变量,我该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!