Vector的内存布局
有人可以解释一下存储在 vector
中的数据的内存布局吗?
就像内存从地址 &myVec[0]
向上有什么布局?它取决于字节顺序吗?所有存储的值的内存都是连续的吗? (我知道 vector
实际上并不存储布尔值)。我可以使用 memcopy 将 vector
的内容转储到文件中以获取我的值的位图吗?
请不要提出诸如“您需要它做什么”之类的问题或诸如使用位集或升压之类的建议。
谢谢你准确的解释
Can someone please explain the memory layout of the data stored in a vector<bool>
?
like what layout does the memory have from address &myVec[0]
upwards? Does it depend on endianness? Is the memory continguous for all stored values? (i'm aware that vector<bool>
doesn't actually store booleans). can I dump the content of a vector<bool>
into a file using memcopy to get a bitmap of my values?
please no questions like "what do you need it for" or suggestions like using bitsets or boost.
Thank you for an accurate explanation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
std::vector 将简单地管理堆上的原始数组。因此,当您执行 &myVec[0] 时,您将获得该数组第一个元素的地址。因为它是一个数组,所以......遵循原始数组的规则。
但是
std::vector 是一个特殊情况,一个特定的实现,C++ 委员会的一个错误,它不是
bool
而是一个管理位的容器。所以避免使用这个。The std::vector will simply manage a raw array on the heap. So whan you do &myVec[0] you get the address of the first element of this array. As it's an array it...follows the rules of a raw array..
BUT
std::vector is a special case, a specific implementation, a mistake of the C++ commitee that is NOT a vector of
bool
but a container managing bits. So avoid using this one.向量本质上是数组的包装器,所以是的,内存是连续的。这也意味着您可以对其使用 memcpy (如果这是您想要的)。
每个元素的字节顺序取决于您当前的架构。
在记忆中会出现这样的情况:
我不确定这是否是您要问的。
A vector is essentially a wrapper around an array so yes, the memory is contiguous. This also means you can use memcpy on it (if that's what you want).
The endianness of each element depends on your current architecture.
would appear like this in memory:
I'm not sure if that is what you are asking.