Vector的内存布局

发布于 2024-10-01 02:52:29 字数 324 浏览 1 评论 0原文

有人可以解释一下存储在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恍梦境° 2024-10-08 02:52:29

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.

吹泡泡o 2024-10-08 02:52:29

向量本质上是数组的包装器,所以是的,内存是连续的。这也意味着您可以对其使用 memcpy (如果这是您想要的)。

每个元素的字节顺序取决于您当前的架构。

vector<bool> myvector;
myvector.push_back(1);
myvector.push_back(0);
myvector.push_back(0);
myvector.push_back(1);
myvector.push_back(0);
myvector.push_back(1);
myvector.push_back(1);
myvector.push_back(0);

在记忆中会出现这样的情况:

1 0 0 1 0 1 1 0

我不确定这是否是您要问的。

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.

vector<bool> myvector;
myvector.push_back(1);
myvector.push_back(0);
myvector.push_back(0);
myvector.push_back(1);
myvector.push_back(0);
myvector.push_back(1);
myvector.push_back(1);
myvector.push_back(0);

would appear like this in memory:

1 0 0 1 0 1 1 0

I'm not sure if that is what you are asking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文